Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active June 14, 2016 09:12
Show Gist options
  • Save kurozumi/86ce73b46175b3b99894 to your computer and use it in GitHub Desktop.
Save kurozumi/86ce73b46175b3b99894 to your computer and use it in GitHub Desktop.
【Python】Seleniumを使ってGoogle検索結果のページ送り(ページネーション)をクリックして次のページヘ進む方法
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
# 標準入力の値を取得
keyword = raw_input()
driver = webdriver.Firefox()
driver.implicitly_wait(10)
# Googleにアクセス
driver.get("https://www.google.co.jp")
elem = driver.find_element_by_name("q");
# キーワード入力
elem.send_keys(keyword)
# 検索
elem.send_keys(Keys.RETURN)
while True:
try:
# 次のページのリンク要素を探す
next = driver.find_element_by_css_selector("#navcnt table td.cur + td a")
# 見つかったらクリック
next.click()
except:
# 見つからなかったら終了
driver.close()
# 3秒待つ
time.sleep(3)
driver.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment