やりたいこと
https://teratail.com/questions/187243
こちらに引き続いての質問になります。
検索結果を取得することはできたのですが次の課題として30件分取得することを試みています
他の言語ので実装されている記事は見つけたので参考にはしていたのですが(以下の記事です)
https://teratail.com/questions/167373
該当コードに関しては以下の通りです
def scraping(keyword) submit_keyword(keyword) @agent.page.search('div.g').map do |node| title = node.search('a') next if title.empty? query = URI.decode_www_form(URI(title.attr("href")).query) url = query[0][1] snipped = node.search('div.s > span.st') next if snipped.empty? || snipped.children.empty? { url: url, title: expect_tag(title.children.to_html), snipped: expect_tag(snipped.children.to_html) } end.reject do |list| list.nil? end end private def submit_keyword(keyword) @agent = Mechanize.new @agent.user_agent_alias = 'Linux Mozilla' @agent.get('https://www.google.co.jp/') @agent.page.form_with(name: 'f') do |form| form.q = keyword end.submit end def expect_tag(string) string.gsub(/(<b>|</b>|<br>|</br>|\R)/, '') end
@agent.getの部分で要素を確定させようと思いまして上記の記事を参考にしたのですがいい結果は得られずでして何かいい記事等ご存知でしたらご教授いただけますと幸いです。
よろしくお願い致します
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/04 05:04
退会済みユーザー
2019/05/04 08:05
2019/05/04 08:31