Amazonのレビュー欄のデータ(レビューの本文や評価など)をスクレイピングすることには成功したのですが、それをcsvに書き込もうとしても一部しか書き込まれません。
取得したデータすべてをcsvに書き込むにはどうしたら教えていただけたら幸いです。
よろしくお願いいたします。
* coding:utf-8 *
require 'anemone'
require 'nokogiri'
require 'open-uri'
require 'kconv'
require 'csv'
urls = ['http://www.amazon.co.jp/product-reviews/ISBNの番号/ref=cm_cr_pr_btm_link_1?ie=UTF8&showViewpoints=0&sortBy=recent&reviewerType=all_reviews&formatType=all_formats&filterByStar=all_stars&pageNumber=1']
opts = {
:depth_limit => false,
:obey_robots_txt => true,
:delay => 3
}
Anemone.crawl(urls, opts) do |anemone|
anemone.skip_links_like /next_+|prev+|unsticky/
anemone.focus_crawl do |page| page.links.keep_if { |link| link.to_s.match( /product-reviews\/ISBNの番号\/ref=cm_cr_pr_btm_link_/) } end PATTERN = %r[ref=cm_cr_pr_btm_link_] anemone.on_pages_like(PATTERN) do |page| doc = Nokogiri::HTML.parse(page.body.toutf8) retxt = [] tit = [] hyo = [] retim = [] renam = [] san = [] reviewtext = doc.xpath("//span[@class='a-size-base review-text']") title = doc.xpath("//a[@class='a-size-base a-link-normal review-title a-color-base a-text-bold']") hyoka = doc.xpath("//div[@class='a-section review']/div[2]/a[1]/i/span") reviewtime = doc.xpath("//div[@class='a-section review']/div[3]/span[4]") reviewname = doc.xpath("//*[@class='a-section review']/div[3]/span[1]/a") sankou = doc.xpath("//*[@class='a-section review']/div[1]/span") reviewtext.each do |node| retxt << node.text end title.each do |node| tit << node.text end hyoka.each do |node| hyo << node.text end reviewtime.each do |node| retim << node.text end reviewname.each do |node| renam << node.text end sankou.each do |node| san << node.text end puts sankou.text, reviewtext.text, title.text, hyoka.text, reviewtime.text, reviewname.text, sankou.text File.open('hibana2.csv', 'w') do |csv| retxt.length.times do |i| csv << [retxt[i], tit[i], hyo[i], retim[i], renam[i], san[i]] end end end
end
#sankou = doc.xpath("//div[@style='margin-left:0.5em;']/div[@style='margin-bottom:0.5em;']").text
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/07/31 15:02
2015/07/31 23:18