From 7df52c95e10a56b31dc61eed095f868ec9e968e1 Mon Sep 17 00:00:00 2001 From: Karen Zeng Date: Tue, 9 May 2023 19:02:36 -0400 Subject: [PATCH] Check the availability & time delay --- fp2_11.py | 10 ++--- fp2_6.py | 120 +++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 97 insertions(+), 33 deletions(-) diff --git a/fp2_11.py b/fp2_11.py index 2dde25c..ca5bde3 100644 --- a/fp2_11.py +++ b/fp2_11.py @@ -32,7 +32,7 @@ driver.delete_all_cookies driver.get(url) # time.sleep(5) -time.sleep(5) +time.sleep(2) # Close the cookies setting window try: cookie_button = driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div[3]/button") @@ -41,13 +41,13 @@ try: except: pass -time.sleep(5) +time.sleep(1) # Clear any modal dialog try: modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]") print(f"modal_close = {modal_close}") modal_close.click() - time.sleep(2) + #time.sleep(2) except: pass @@ -60,7 +60,7 @@ def callback(message): try: try: - time.sleep(5) + time.sleep(2) # Clear any modal dialog modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]") # print(f"modal_close = {modal_close}") @@ -100,7 +100,7 @@ def callback(message): time.sleep(2) try: - time.sleep(5) + time.sleep(2) # Clear any notice notice_close = driver.find_element(By.XPATH, "/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[1]/div[2]/button") print(f"notice_close = {notice_close}") diff --git a/fp2_6.py b/fp2_6.py index cfc09ec..5996625 100644 --- a/fp2_6.py +++ b/fp2_6.py @@ -10,43 +10,107 @@ options.add_argument('--disable-gpu') driver = webdriver.Chrome(options=options) # Define the zip codes for which to extract the price -zip_codes = ['10001', '90001', '90210'] +zip_codes = ['85305', '30313', '21230', '14127','28202','60605','45202','44114','76011','80204','48226','54304','77054','46225','32202','64129','89118','90301','33056','55415','02035','70112','07073','19148','15212','95054','98134','33607','37213','20785'] # Load the page and extract the __NEXT_DATA__ data url = 'https://www.sayweee.com/en/product/Want-Want-Senbei-Rice-Crackers/17119' +#url = 'https://www.sayweee.com/en/product/Lays-Potato-Chips-Cucumber-Flavor/51261' +#url = 'https://www.sayweee.com/en/product/Glico-Pocky-Cookies-and-Cream-Family-Pack/79467' +#url = 'https://www.sayweee.com/en/product/Xiang-Na-Lan-Thai-Jasmine-Rice/19566?category=dried01' +#url = 'https://www.sayweee.com/en/product/Sekka-Premium-Japanese-Medium-Grain-Rice/81224' +#url = 'https://www.sayweee.com/en/product/Big-Green-Spring-Farm-Organic-Millet/90991?category=dried02' +#url = 'https://www.sayweee.com/en/product/Liuzhou-Guangxi-Specialty-LuoSiFen-Hot-Spicy-Noodles/95442?category=instant01' +#url = 'https://www.sayweee.com/en/product/Sanyangyishi-Fresh-Instant-Rice-Noodles/2156?category=instant01' +#url = 'https://www.sayweee.com/en/product/Nissin-Beef-Flavor-Cup-Noodles/102519?category=instant01' +#url = 'https://www.sayweee.com/en/product/Formosa-Pork-Sung-/23528?category=canned04' + +driver.delete_all_cookies driver.get(url) +time.sleep(2) + +# Close the cookies setting window +try: + cookie_button = driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div[3]/button") + print(f"cookie_button = {cookie_button}") + cookie_button.click() +except: + pass + time.sleep(5) +# Clear any modal dialog +try: + modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]") + print(f"modal_close = {modal_close}") + modal_close.click() + time.sleep(2) +except: + pass -for zip_code in zip_codes: - # Enter the zip code and click the "Check" button - zip_code_input = driver.find_element(By.XPATH, '//*[@id="rowToolRef"]/div[2]/div[1]/div/div/div[1]') - zip_code_input.clear() - zip_code_input.send_keys(zip_code) - check_button = driver.find_element(By.XPATH, '/html/body/div[4]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div/div/div[1]/div/div/div[3]/div/button') - check_button.click() - - # Wait for the price to load and extract it - time.sleep(5) - next_data_element = None +def callback(zip_code): try: - next_data_element = driver.find_element(By.XPATH, "//script[@type='application/ld+json']") - except: - print('__NEXT_DATA__ element not found') + try: + time.sleep(5) + # Clear any modal dialog + modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]") + # print(f"modal_close = {modal_close}") + modal_close.click() + time.sleep(2) + except: + pass - if next_data_element is not None: - next_data_text = next_data_element.get_attribute('innerHTML') - next_data_start_index = next_data_text.index('{') - next_data_end_index = next_data_text.rindex('}') + 1 - next_data_json = next_data_text[next_data_start_index:next_data_end_index] - next_data = json.loads(next_data_json) - # Extract the price from the __NEXT_DATA__ data - price = next_data['offers']['price'] + try: + time.sleep(1) + # Clear any notice + notice_close = driver.find_element(By.XPATH, "/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[1]/div[2]/button") + # print(f"notice_close = {notice_close}") + notice_close.click() + time.sleep(1) + except: + pass - # Print the price for the current zip code - print(f"Price for zip code {zip_code}: {price}") - else: + # Enter zip code in the input field and click the "Check" button + zip_button = driver.find_element(By.XPATH, '/html/body/div[2]/div/header/div[3]/div/div/div[2]/div[1]') + # print(f"{zip_button:}") + zip_button.click() + time.sleep(1) + + zip_input = driver.find_element(By.XPATH, '//div/label/following-sibling::input') + # print(f"{zip_input:}") + zip_input.click() + # print("clearing zip_input") + zip_input.send_keys("\b\b\b\b\b") + # print("sending zip code") + zip_input.send_keys(zip_code) + + check_button = driver.find_element(By.XPATH, "//button[text() = 'Confirm']") + # print(f"{check_button:}") + check_button.click() + time.sleep(2) + + try: + time.sleep(5) + # Clear any notice + notice_close = driver.find_element(By.XPATH, "/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[1]/div[2]/button") + print(f"notice_close = {notice_close}") + notice_close.click() + time.sleep(2) + except: + pass + + # Extract the price of rice crackers for this zip code + try: + sold_out_notice = driver.find_element(By.XPATH, '//*[@id="soldStatusNotice"]') + print(f"No such product in this area = {sold_out_notice}") + notice_close.click() + except: + print(f"The product is available in this area {zip_code}") + + except Exception as e: + print(f"Caught exception: {e}") print(f"Price not found for zip code {zip_code}") -# Close the Selenium driver -driver.quit() +for zip_code in zip_codes: + callback(zip_code) + +driver.quit() \ No newline at end of file