Created
March 25, 2016 00:32
-
-
Save kurozumi/8565279e8cb7cd7d519b 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 | |
# Firefox起動 | |
driver = webdriver.Firefox() | |
# googleへ | |
driver.get("https://www.google.co.jp?num=100") | |
domain = "a-zumi.net" | |
while True: | |
# 標準入力の値を取得 | |
keyword = raw_input("入力: ").decode('utf-8') | |
# 入力フォームの値をクリアにする | |
driver.find_element_by_name("q").clear() | |
# 入力フォームの要素を探す | |
element = driver.find_element_by_name("q") | |
# 入力フォームにキーワードを入力する | |
element.send_keys(keyword) | |
# フォームを送信する | |
element.send_keys(Keys.RETURN) | |
# titleタグに標準入力したキーワードが含まれるまで10秒待つ | |
WebDriverWait(driver, 10).until( | |
EC.title_contains(keyword) | |
) | |
elements = driver.find_elements_by_css_selector("#ires h3 a") | |
for i, e in enumerate(elements): | |
if domain in e.get_attribute("href"): | |
print "%s位にインデックスされています。" % (i+1) | |
break | |
driver.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment