GoToEat 오사카 캠페인 페이지가 롤업되어 csv 파일로 작성됨
개요
고투잇 오사카의 홍보 이벤트 페이지에는 점포가 검색하는 페이지가 있지만, 지도를 표시할 수는 없다.
https://goto-eat.weare.osaka-info.jp/gotoeat/
Google Map API를 사용하면 지도를 표시할 수 있지만, 그 전에 대상점을 표로 보관해야 합니다.
루비를 사용한 노코지리는 도장했다.
코드 require 'open-uri'
require 'nokogiri'
require "csv"
@id=0
@page=1
@hashes=[]
# GoToEat大阪のページ(1ページ目)
@url = 'https://goto-eat.weare.osaka-info.jp/?search_element_0_0=2&search_element_0_1=3&search_element_0_2=4&search_element_0_3=5&search_element_0_4=6&search_element_0_5=7&search_element_0_6=8&search_element_0_7=9&search_element_0_8=10&search_element_0_9=11&search_element_0_cnt=10&search_element_1_cnt=18&searchbutton=%E5%8A%A0%E7%9B%9F%E5%BA%97%E8%88%97%E3%82%92%E6%A4%9C%E7%B4%A2%E3%81%99%E3%82%8B&csp=search_add&feadvns_max_line_0=2&fe_form_no=0'
def scraping(page)
# ページを開き、htmlを読み込んで変数htmlに渡す
html = open(@url) {|f| f.read}
charset = "utf8"
# htmlをパース(解析)してオブジェクトを作成
doc = Nokogiri::HTML.parse(html, nil, charset)
doc.xpath('/html/body/div/div[1]/main/section/div/div/ul/li').each { |node|
# データ格納庫であるhashを作成
hash=Hash.new(nil)
keys=[:id,:name,:address,:tel,:open,:close,:category1,:category2]
keys.each {|key| hash.store(key,nil)}
hash.store(:id,@id)
hash.store(:name,node.xpath("p").inner_text)
trs=node.xpath("table").xpath("tr")
address=trs[0].xpath("td").inner_text
address.gsub!("\r\n","")
address.gsub!(/[[:space:]]/,"")
hash.store(:address,address)
hash.store(:tel,trs[1].xpath("td").inner_text)
hash.store(:open,trs[2].xpath("td").inner_text)
hash.store(:close,trs[3].xpath("td").inner_text)
categories=node.xpath("ul").xpath("li")
hash.store(:category1,categories[0].inner_text) if categories[0]
hash.store(:category2,categories[1].inner_text) if categories[1]
@id+=1
@hashes << hash
CSV.open("GoToEatOsaka.csv", "a", headers: hash.keys) {|csv|
csv << hash.values
}
}
# 次のページを探します
as=doc.xpath("/html/body/div/div[1]/main/section/div/div/div[2]").xpath("a")
as.each{|a|
content=a.get_attribute("title")
if content=="Page #{page}" then
puts page
@url=a.attribute("href")
break
end
}
end
(2..221).each{|page|
scraping(page)
}
실행 결과
csv로 잘 저장되어 있습니다.
Reference
이 문제에 관하여(GoToEat 오사카 캠페인 페이지가 롤업되어 csv 파일로 작성됨), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/w5966qzh/items/8014756557165ef53f4f
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
require 'open-uri'
require 'nokogiri'
require "csv"
@id=0
@page=1
@hashes=[]
# GoToEat大阪のページ(1ページ目)
@url = 'https://goto-eat.weare.osaka-info.jp/?search_element_0_0=2&search_element_0_1=3&search_element_0_2=4&search_element_0_3=5&search_element_0_4=6&search_element_0_5=7&search_element_0_6=8&search_element_0_7=9&search_element_0_8=10&search_element_0_9=11&search_element_0_cnt=10&search_element_1_cnt=18&searchbutton=%E5%8A%A0%E7%9B%9F%E5%BA%97%E8%88%97%E3%82%92%E6%A4%9C%E7%B4%A2%E3%81%99%E3%82%8B&csp=search_add&feadvns_max_line_0=2&fe_form_no=0'
def scraping(page)
# ページを開き、htmlを読み込んで変数htmlに渡す
html = open(@url) {|f| f.read}
charset = "utf8"
# htmlをパース(解析)してオブジェクトを作成
doc = Nokogiri::HTML.parse(html, nil, charset)
doc.xpath('/html/body/div/div[1]/main/section/div/div/ul/li').each { |node|
# データ格納庫であるhashを作成
hash=Hash.new(nil)
keys=[:id,:name,:address,:tel,:open,:close,:category1,:category2]
keys.each {|key| hash.store(key,nil)}
hash.store(:id,@id)
hash.store(:name,node.xpath("p").inner_text)
trs=node.xpath("table").xpath("tr")
address=trs[0].xpath("td").inner_text
address.gsub!("\r\n","")
address.gsub!(/[[:space:]]/,"")
hash.store(:address,address)
hash.store(:tel,trs[1].xpath("td").inner_text)
hash.store(:open,trs[2].xpath("td").inner_text)
hash.store(:close,trs[3].xpath("td").inner_text)
categories=node.xpath("ul").xpath("li")
hash.store(:category1,categories[0].inner_text) if categories[0]
hash.store(:category2,categories[1].inner_text) if categories[1]
@id+=1
@hashes << hash
CSV.open("GoToEatOsaka.csv", "a", headers: hash.keys) {|csv|
csv << hash.values
}
}
# 次のページを探します
as=doc.xpath("/html/body/div/div[1]/main/section/div/div/div[2]").xpath("a")
as.each{|a|
content=a.get_attribute("title")
if content=="Page #{page}" then
puts page
@url=a.attribute("href")
break
end
}
end
(2..221).each{|page|
scraping(page)
}
실행 결과
csv로 잘 저장되어 있습니다.
Reference
이 문제에 관하여(GoToEat 오사카 캠페인 페이지가 롤업되어 csv 파일로 작성됨), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/w5966qzh/items/8014756557165ef53f4f
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(GoToEat 오사카 캠페인 페이지가 롤업되어 csv 파일로 작성됨), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/w5966qzh/items/8014756557165ef53f4f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)