Skip to content

Instantly share code, notes, and snippets.

@t-kashima
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save t-kashima/6e378c0b5f9317858d5f to your computer and use it in GitHub Desktop.

Select an option

Save t-kashima/6e378c0b5f9317858d5f to your computer and use it in GitHub Desktop.
MechanizeでGoogle検索するあれ
# coding: utf-8
require 'mechanize'
agent = Mechanize.new
page = agent.get('http://google.co.jp')
page = page.form_with(:name => 'f') do |form|
form.q = 'UNUUU Foundation'
end.submit
# UNUUU Foundationのリンクを取得する
pages = page.links_with(:href => /.+unuuu.+/)
# ページをフィルタリングする
pages = pages.select{|page|
# キャッシュは取り除く
page.text !~ /キャッシュ/
}
# Googleで検索したUNUUU Foundationのリンク
for page in pages do
p page.text + ' : ' + page.href
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment