Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
【Python】Seleniumを使ってアマゾンの検索結果から予約商品のみを取得する方法
# coding: utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
driver = webdriver.Firefox()
driver.get("http://www.amazon.co.jp/")
while True:
# 標準入力でキーワードを入力
keyword = raw_input("キーワード入力: ").decode('utf-8')
# 検索フォームが表示されるまで10秒待つ
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "field-keywords"))
)
# 検索フォームのテキストをクリア
driver.find_element_by_name("field-keywords").clear()
# 検索フォームにキーワードを入力
element.send_keys(keyword)
# 検索実行
element.send_keys(Keys.RETURN)
# 並び替えセレクトボックスを探して最新商品順を選択
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "select#sort"))
)
Select(element).select_by_value("date-desc-rank")
# ページ送りの最後まで予約受付中の商品を探す。
while True:
elements = driver.find_elements_by_css_selector("#atfResults ul li, #mainResults ul li")
for element in elements:
if u"予約受付中" in element.text:
print element.text
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.LINK_TEXT, u"次のページ"))
)
except Exception as e:
print e
break
element.click()
driver.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment