require 'selenium-webdriver'
# ブラウザ起動
driver = Selenium::WebDriver.for :firefox
# サイトマップページにアクセス
driver.navigate.to "https://www.example.com/sitemap"
# ページ内のリンク取得
elements = driver.find_elements(:tag_name, 'a')
links = elements.map{|a| a.attribute('href')}
links.uniq! #重複削除
links.each.with_index(1) do |url,i|
sleep 2 #2秒待つ
driver.navigate.to url
puts url
# title 出力
puts driver.title
# meta description 出力
puts driver.find_element(:xpath, "//meta[@name='description']").attribute('content')
# meta keywords 出力
puts driver.find_element(:xpath, "//meta[@name='keywords']").attribute('content')
# 画面キャプチャ
#driver.save_screenshot('filename.png')
# 100件で終了
break if i == 100
end
# ブラウザ終了
driver.quit
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme