From 2ead943e3a41398dc0e070714a4b2041f05a045c Mon Sep 17 00:00:00 2001 From: Karen Zeng Date: Thu, 11 May 2023 12:50:33 -0400 Subject: [PATCH] Cleaned up the exception and Added product_id --- fp2_10.py | 6 ++--- fp2_11.py | 76 ++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/fp2_10.py b/fp2_10.py index 0517181..baf21ef 100644 --- a/fp2_10.py +++ b/fp2_10.py @@ -4,8 +4,8 @@ import os import asyncio # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "C:/Users/Karen/AppData/Roaming/gcloud/application_default_credentials.json" -# project_id = "fp-20230425" -project_id = "svig-384801" +project_id = "fp-20230425" +# project_id = "svig-384801" ''' topic_name = "fp-topic-NFL-zipcodes" @@ -41,7 +41,7 @@ subscription = subscriber.create_subscription( # Publish a message to the topic # Read the data from the csv file # file_path = "C:/Users/Karen/Desktop/zipcodes.csv" -file_path = "zipcodes.csv" +file_path = "C:/Users/Karen/Desktop/zipcodes.csv" with open(file_path, "r") as csv_file: reader = csv.reader(csv_file) diff --git a/fp2_11.py b/fp2_11.py index ca5bde3..283dbff 100644 --- a/fp2_11.py +++ b/fp2_11.py @@ -8,11 +8,13 @@ import time import os from concurrent.futures import TimeoutError import pandas as pd +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "C:/Users/Karen/AppData/Roaming/gcloud/application_default_credentials.json" # Create an empty dataframe to store the prices -df = pd.DataFrame(columns=['Zip Code', 'Price']) +df = pd.DataFrame(columns=['Product ID', 'Zip Code', 'Price']) print(f"Created a dataframe: {df}") # Configure the Selenium driver options @@ -28,23 +30,30 @@ subscription_path = subscriber.subscription_path(project_id, subscription_name) # Load the product page and extract the __NEXT_DATA__ data url = 'https://www.sayweee.com/en/product/Want-Want-Senbei-Rice-Crackers/17119' +product_id = url.split("/")[-1] driver.delete_all_cookies driver.get(url) # time.sleep(5) -time.sleep(2) +# 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") + # cookie_button = driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div[3]/button") + cookie_button = WebDriverWait(driver, 2).until( + EC.presence_of_element_located((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(1) +# time.sleep(1) # Clear any modal dialog try: - modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]") + # modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]") + modal_close = WebDriverWait(driver, 2).until( + EC.presence_of_element_located((By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]")) + ) print(f"modal_close = {modal_close}") modal_close.click() #time.sleep(2) @@ -60,33 +69,45 @@ def callback(message): try: try: - time.sleep(2) + # time.sleep(2) # Clear any modal dialog - modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]") + # modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]") + modal_close = WebDriverWait(driver, 2).until( + EC.presence_of_element_located((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 try: - time.sleep(1) + # 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") + # notice_close = driver.find_element(By.XPATH, "/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[1]/div[2]/button") + notice_close = WebDriverWait(driver, 5).until( + EC.presence_of_element_located((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) + # time.sleep(1) except: pass # 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]') + # zip_button = driver.find_element(By.XPATH, '/html/body/div[2]/div/header/div[3]/div/div/div[2]/div[1]') + zip_button = WebDriverWait(driver, 5).until( + EC.presence_of_element_located((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) + # time.sleep(1) - zip_input = driver.find_element(By.XPATH, '//div/label/following-sibling::input') + # zip_input = driver.find_element(By.XPATH, '//div/label/following-sibling::input') + zip_input = WebDriverWait(driver, 2).until( + EC.presence_of_element_located((By.XPATH, '//div/label/following-sibling::input')) + ) # print(f"{zip_input:}") zip_input.click() # print("clearing zip_input") @@ -95,23 +116,33 @@ def callback(message): zip_input.send_keys(zip_code) check_button = driver.find_element(By.XPATH, "//button[text() = 'Confirm']") + check_button = WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.XPATH,"//button[text() = 'Confirm']"))) # print(f"{check_button:}") check_button.click() - time.sleep(2) + # time.sleep(2) try: - time.sleep(2) + # 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}") + # notice_close = driver.find_element(By.XPATH, "/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[1]/div[2]/button") + notice_close = WebDriverWait(driver, 2).until(EC.presence_of_element_located((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) + #time.sleep(2) + except: + pass + + try: + # Clear any modal dialog + modal_close = WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]"))) + # print(f"modal_close = {modal_close}") + modal_close.click() except: pass # Extract the price of rice crackers for this zip code - + ''' price_element = driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/div[1]/div/div/div[2]/div[2]/div[1]/div[4]/div[1]/div') price = price_element.text.strip()[1:] @@ -130,7 +161,7 @@ def callback(message): subscriber.acknowledge(ack_ids = [message.ack_id], subscription = subscription_path) # acknowledge receipt of message # Append the zip code and price to the dataframe #append_to_df(zip_code, price) - new_row = pd.DataFrame({'Zip Code': [zip_code], 'Price': [price]}) + new_row = pd.DataFrame({'Product ID': [product_id], 'Zip Code': [zip_code], 'Price': [price]}) return pd.concat([df, new_row], ignore_index=True) #print(f"Recorded the zip code and price: {zip_code},{price}") #print(f"Updated a dataframe: {df}") @@ -141,8 +172,6 @@ def callback(message): price = 'Price not found' return df -import time - # Start receiving messages (sync version) while True: pull_request = pubsub_v1.types.PullRequest(max_messages = 1, subscription = subscription_path) @@ -158,6 +187,7 @@ while True: print(f"Updated a dataframe: {df}") else: print(f"no messages, trying again at {time.asctime()}") + break # Start receiving messages (async version) #flow_control = pubsub_v1.types.FlowControl(max_messages = 1)