KimuraiとセレンをフィーチャーしたRubyによるウェブ掻き取りに関するノート
18508 ワード
私は以前、バスケットボールのリファレンスからいくつかの統計情報を削ることについて書いていたと私は再びそれを行うに戻ったとき私のリンクは動作しませんでした、私は仕事の一部をやり直す必要があることを意味します.私は特にノコギリとスクレーピングで私の最後の探査を愛していないので、私は機会を私の知識を学び、展開する機会としてこれを取った.
私は2008年以来、ウォルクソールとしてこれを書くつもりはないthis blog 本当にそれを通してあなたを歩くでしょう、そして、本当にちょうどあなたが必要とする情報を得ることができる異なる方法を示すために、これを掲示してください.
CSSを使っている要素を見つけることは、XPathをハードコーディングするより安定したアプローチにつながります.クラス名またはIDが同じである限り、サイトが要素をネストしてコードを壊してしまうと、問題になりません.
私が困ったことをしようとしている多くの時間を費やした1つのものは、私の変数がすべてそれが一見して正しいことをしているのに、空になっていたということでした.結局、私はサイトがロードを終えなかったならば、それを理解しました.私は、ちょうどこれを使用するより安全な方法があると仮定します
マイコード
私は2008年以来、ウォルクソールとしてこれを書くつもりはないthis blog 本当にそれを通してあなたを歩くでしょう、そして、本当にちょうどあなたが必要とする情報を得ることができる異なる方法を示すために、これを掲示してください.
CSSを使っている要素を見つけることは、XPathをハードコーディングするより安定したアプローチにつながります.クラス名またはIDが同じである限り、サイトが要素をネストしてコードを壊してしまうと、問題になりません.
私が困ったことをしようとしている多くの時間を費やした1つのものは、私の変数がすべてそれが一見して正しいことをしているのに、空になっていたということでした.結局、私はサイトがロードを終えなかったならば、それを理解しました.私は、ちょうどこれを使用するより安全な方法があると仮定します
sleep 2
または何か.マイコード
require 'kimurai'
require 'json'
class TequilaScraper < Kimurai::Base
@name = 'tqdb_scrap'
@start_urls = ['https://tequilamatchmaker.com/tequilas/2325-fortaleza-blanco']
@engine = :selenium_chrome
@@tequilas = []
def scrape_page
sleep 2
doc = browser.current_response
tequila = doc.css('div.product-actions')
teq_name = tequila.css('h1[itemprop="name"]').text.gsub(/\n/, "")
teq_type = tequila.css('div.product-type a').text.gsub(/\n/, "")
teq_rating_p = tequila.css('ul.product-list__item__ratings li')[0].text.gsub(/\D/, '').gsub(/\n/, "")
teq_rating_c = tequila.css('ul.product-list__item__ratings li')[1].text.gsub(/\D/, '').gsub(/\n/, "")
teq_price_check = tequila.css('div.commerce-price-container div span')[1]
if teq_price_check
teq_price = tequila.css('div.commerce-price-container div span')[1].text.gsub(/\n/, "")
else
teq_price = 'n/a'
end
doc_mid = doc.css('div.container')
teq_image = doc_mid.css('img.product-image').attr('src')
teq_nom = doc_mid.css('div.production-details_product table tbody tr')[0].css('td a').text.gsub(/\n/, "")
doc_mid.search('span.sr-only').each do |spans|
# remove search result spans since it's just a comma
spans.remove
end
teq_agave = doc_mid.css('div.production-details_product table tbody tr')[1].css('td').text.gsub(/\n/, "")
teq_agave_region = doc_mid.css('div.production-details_product table tbody tr')[2].css('td').text.gsub(/\n/, "")
teq_region = doc_mid.css('div.production-details_product table tbody tr')[3].css('td').text.gsub(/\n/, "")
teq_cooking = doc_mid.css('div.production-details_product table tbody tr')[4].css('td').text.gsub(/\n/, "")
teq_extraction = doc_mid.css('div.production-details_product table tbody tr')[5].css('td').text.gsub(/\n/, "")
teq_water = doc_mid.css('div.production-details_product table tbody tr')[6].css('td').text.gsub(/\n/, "")
teq_fermentation = doc_mid.css('div.production-details_product table tbody tr')[7].css('td').text.gsub(/\n/, "")
teq_distillation = doc_mid.css('div.production-details_product table tbody tr')[8].css('td').text.gsub(/\n/, "")
teq_still = doc_mid.css('div.production-details_product table tbody tr')[9].css('td').text.gsub(/\n/, "")
teq_aging = doc_mid.css('div.production-details_product table tbody tr')[10].css('td').text.gsub(/\n/, "")
teq_abv = doc_mid.css('div.production-details_product table tbody tr')[11].css('td').text.gsub(/\n/, "")
teq_other = doc_mid.css('div.production-details_product table tbody tr')[12].css('td').text.gsub(/\n/, "")
tequila = {name: teq_name, type: teq_type, rating_p: teq_rating_p, rating_c: teq_rating_c,
price: teq_price, image_url: teq_image, nom: teq_nom, agave: teq_agave, agave_region: teq_agave_region,
region: teq_region, cooking: teq_cooking, extraction: teq_extraction, water: teq_water, fermentation: teq_fermentation,
distillation: teq_distillation, still: teq_still, aging: teq_aging, abv: teq_abv, other: teq_other}
@@tequilas << tequila #if !@@tequilas.include?(tequila)
end
def parse(response, url:, data: {})
scrape_page
File.open("tequila.json","w") do |f|
f.write(JSON.pretty_generate(@@tequilas))
end
@@tequilas
end
end
TequilaScraper.crawl!
puts 'done scraping'
当初、私は反応サイトから模擬データを得るつもりでしたが、簡単にテストするために、このテキーラデータベースを選びました.セレンを使用すると、これは少しoverkillですが、すべてのすべてのそれは本当によく働いた.次回は、いくつかのパイソンまたはJSの製紙に掘るしようとするつもりだと思う.Reference
この問題について(KimuraiとセレンをフィーチャーしたRubyによるウェブ掻き取りに関するノート), 我々は、より多くの情報をここで見つけました https://dev.to/denvermullets/some-notes-on-web-scraping-with-ruby-featuring-kimurai-and-selenium-5a9pテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol