Last active
August 4, 2016 15:36
-
-
Save kurozumi/e623e707268713d5a0acf175254a4ff0 to your computer and use it in GitHub Desktop.
【Python】Amazonで指定したキーワードの予約商品を探す
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
# codig: utf-8 | |
from selenium import webdriver | |
from selenium.webdriver.common.keys import Keys | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support.select import Select | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support import expected_conditions as EC | |
import click | |
class Firefox(object): | |
def __init__(self): | |
self.__driver = webdriver.Firefox() | |
def __enter__(self): | |
return self.__driver | |
def __exit__(self, exception_type, exception_value, traceback): | |
self.__driver.close() | |
@click.command() | |
@click.argument("keyword") | |
def search(keyword): | |
with Firefox() as browser: | |
browser.get("https://www.amazon.co.jp/") | |
elem = browser.find_element_by_name("field-keywords") | |
elem.clear() | |
elem.send_keys(keyword) | |
elem.send_keys(Keys.RETURN) | |
elem = WebDriverWait(browser, 5).until( | |
EC.presence_of_element_located((By.CSS_SELECTOR, 'select[name="sort"]')) | |
) | |
Select(elem).select_by_value("date-desc-rank") | |
while True: | |
elems = browser.find_elements_by_css_selector("#atfResults ul li, #mainResults ul li") | |
for elem in elems: | |
if "予約受付中" in elem.text and keyword in elem.text: | |
print(elem.text) | |
try: | |
elem = WebDriverWait(browser, 5).until( | |
EC.presence_of_element_located((By.LINK_TEXT, "次のページ")) | |
) | |
elem.click() | |
except: | |
break | |
if __name__ == "__main__": | |
search() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment