現在、Rubyを用いてAmazonサイトから書籍のタイトル、画像のクローリングを行っています。
そこで、
Amazon.co.jp: 文芸作品 - 文学・評論: 本
上記のページ内の作品名は抽出することは出来ました。
しかし、次のページへクローリングさせる方法がわかりません。
Rubyのライブラリでnokogiriとanemoneを使って、
プログラムとしては階層値に1をいれanemone.focus_crawlメソッドを用いて2ページ目だけに行くように絞り込みました。
require 'anemone' require 'nokogiri' require 'kconv' #def save_image(url) opts = { :proxy_host => 'proxy.ksc.kwansei.ac.jp', :proxy_port => '8080', depth_limit:1 } urls = [] #文学評論 urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_0?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A548206&bbn=466284&ie=UTF8&qid=1479178349&rnid=466284") #文芸作品 urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_1?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A507156&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284") #歴史時代小説 urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_2?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A507214&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284") #経済社会小説 urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_3?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A507216&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284") #ミステリー urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_4?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A507300&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284") #SF urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_5?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A507072&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284") #ロマンス urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_6?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A467258&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284") #エッセー urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_7?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A467260&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284") #古典 urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_8?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A467256&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284") #詩歌 urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_9?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A467266&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284") #戯曲 urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_10?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A467262&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284") #評論 urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_11?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A467264&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284") #伝承 urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_12?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A467252&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284") #全集 Anemone.crawl(urls,opts)do|anemone| #巡回先の絞り込み #ページごとにどのリンク先を巡回するか指定する PATTERN = [%Q[//*[@id="pagnNextLink"]]] anemone.focus_crawl do |page| page.links.keep_if { |link| #link.to_s.match(PATTERN) } end # 正規表現で一致したページのみ処理 #PATTERN = %r[466298\/+|466282\/+|2291657051\/+|2291905051\/+] #anemone.on_pages_like(PATTERN) do |page| #puts page.url #end anemone.on_every_page do |page| doc = Nokogiri::HTML.parse(page.body.toutf8) bodys = doc.xpath(%Q[//*[@id="result_0"]/div/div[3]/div[1]/a/h2]) bodys1 = doc.xpath(%Q[//*[@id="result_1"]/div/div[3]/div[1]/a/h2]) bodys2 = doc.xpath(%Q[//*[@id="result_2"]/div/div[3]/div[1]/a/h2]) bodys3 = doc.xpath(%Q[//*[@id="result_3"]/div/div[3]/div[1]/a/h2]) bodys4 = doc.xpath(%Q[//*[@id="result_4"]/div/div[3]/div[1]/a/h2]) bodys5 = doc.xpath(%Q[//*[@id="result_5"]/div/div[3]/div[1]/a/h2]) bodys6 = doc.xpath(%Q[//*[@id="result_6"]/div/div[3]/div[1]/a/h2]) bodys7 = doc.xpath(%Q[//*[@id="result_7"]/div/div[3]/div[1]/a/h2]) bodys8 = doc.xpath(%Q[//*[@id="result_8"]/div/div[3]/div[1]/a/h2]) bodys9 = doc.xpath(%Q[//*[@id="result_9"]/div/div[3]/div[1]/a/h2]) bodys10 = doc.xpath(%Q[//*[@id="result_10"]/div/div[3]/div[1]/a/h2]) bodys11 = doc.xpath(%Q[//*[@id="result_11"]/div/div[3]/div[1]/a/h2]) bodys12 = doc.xpath(%Q[//*[@id="result_12"]/div/div[3]/div[1]/a/h2]) bodys13 = doc.xpath(%Q[//*[@id="result_13"]/div/div[3]/div[1]/a/h2]) bodys14 = doc.xpath(%Q[//*[@id="result_14"]/div/div[3]/div[1]/a/h2]) bodys15 = doc.xpath(%Q[//*[@id="result_15"]/div/div[3]/div[1]/a/h2]) bodys16 = doc.xpath(%Q[//*[@id="result_16"]/div/div[3]/div[1]/a/h2]) bodys17 = doc.xpath(%Q[//*[@id="result_17"]/div/div[3]/div[1]/a/h2]) bodys18 = doc.xpath(%Q[//*[@id="result_18"]/div/div[3]/div[1]/a/h2]) bodys19 = doc.xpath(%Q[//*[@id="result_19"]/div/div[3]/div[1]/a/h2]) bodys20 = doc.xpath(%Q[//*[@id="result_20"]/div/div[3]/div[1]/a/h2]) bodys21 = doc.xpath(%Q[//*[@id="result_21"]/div/div[3]/div[1]/a/h2]) bodys22 = doc.xpath(%Q[//*[@id="result_22"]/div/div[3]/div[1]/a/h2]) bodys23 = doc.xpath(%Q[//*[@id="result_23"]/div/div[3]/div[1]/a/h2]) bodys24 = doc.xpath(%Q[//*[@id="result_24"]/div/div[3]/div[1]/a/h2]) bodys25 = doc.xpath(%Q[//*[@id="result_25"]/div/div[3]/div[1]/a/h2]) puts bodys.text + "a" puts bodys1.text + "abc" puts bodys2.text + "a" puts bodys3.text + "a" puts bodys4.text + "a" puts bodys5.text + "a" puts bodys6.text + "abcdef" puts bodys7.text + "a" puts bodys8.text + "a" puts bodys9.text + "a" puts bodys10.text + "a" puts bodys11.text + "a" puts bodys12.text + "a" puts bodys13.text + "a" puts bodys14.text + "abcdefgh" puts bodys15.text + "a" puts bodys16.text + "a" puts bodys17.text + "a" puts bodys18.text + "a" puts bodys19.text + "a" puts bodys20.text + "abcdefghijk" puts bodys21.text + "a" puts bodys22.text + "a" puts bodys23.text + "a" puts bodys24.text + "a" puts bodys25.text + "a" puts " " end end
ページ移動の確認を行うために、二ページ目の最初の二つの書籍のタイトルを取ってくるようにしたんですが、どうしても表示されません。
最終的には前ページのクローリングをさせたいと考えています。
解決策をご教授いただけますと幸いです。
どうぞ宜しくお願いいたします。
あなたの回答
tips
プレビュー