Last active
February 25, 2016 09:53
-
-
Save kurozumi/ac0e6bb83b4b27f88728 to your computer and use it in GitHub Desktop.
【Python】Seleniumを使ってコマンドラインから標準入力したキーワードでGoogle検索をして検索結果ページのスクリーンショットを撮影する方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from selenium import webdriver | |
from selenium.webdriver.common.keys import Keys | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
while True: | |
# 標準入力の値を取得 | |
keyword = raw_input() | |
# FireFox起動 | |
driver = webdriver.Firefox() | |
# 10秒のWait設定 | |
driver.implicitly_wait(10) | |
# Googleへ | |
driver.get("https://www.google.co.jp") | |
# 入力フォームを探す | |
element = driver.find_element_by_name("q"); | |
# キーワード入力 | |
element.send_keys(keyword.decode('utf-8')) | |
# 検索実行 | |
element.send_keys(Keys.RETURN) | |
# 検索結果が表示されるまで最大10秒待つ | |
WebDriverWait(driver, 10).until( | |
EC.presence_of_element_located((By.ID, "ires")) | |
) | |
# スクリーンショット撮影 | |
driver.save_screenshot("%s.png" % keyword) | |
# 終了 | |
driver.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment