forked from SVIG/python-bootcamp
Added better xpath selectors for the UI elements
This commit is contained in:
@@ -32,12 +32,13 @@ topic_path = publisher.topic_path(project_id, topic_name)
|
||||
|
||||
# Create a Pub/Sub subscription
|
||||
subscriber = pubsub_v1.SubscriberClient()
|
||||
'''
|
||||
subscription_name = "fp-subscription-zipcodes"
|
||||
subscription_path = subscriber.subscription_path(project_id, subscription_name)
|
||||
subscription = subscriber.create_subscription(
|
||||
name=subscription_path, topic=topic_path
|
||||
)
|
||||
|
||||
'''
|
||||
# Publish a message to the topic
|
||||
# Read the data from the csv file
|
||||
file_path = "C:/Users/Karen/Desktop/zipcodes.csv"
|
||||
@@ -51,7 +52,7 @@ with open(file_path, "r") as csv_file:
|
||||
future = publisher.publish(topic_path, data)
|
||||
print(f"Published message ID {future.result()}")
|
||||
|
||||
|
||||
'''
|
||||
async def receive_messages():
|
||||
response = subscriber.pull(subscription=subscription_path, max_messages=1)
|
||||
for received_message in response.received_messages:
|
||||
@@ -59,3 +60,4 @@ async def receive_messages():
|
||||
|
||||
|
||||
asyncio.run(receive_messages())
|
||||
'''
|
||||
@@ -20,14 +20,14 @@ topic_path = publisher.topic_path(project_id, topic_name)
|
||||
publisher.delete_topic(request={"topic": topic_path})
|
||||
print(f"Topic '{topic_name}' deleted.")
|
||||
|
||||
|
||||
|
||||
'''
|
||||
|
||||
|
||||
# Create a pubsub topic
|
||||
publisher = pubsub_v1.PublisherClient()
|
||||
topic_name = "fp-topic-NFL-zipcodes"
|
||||
topic_path = publisher.topic_path(project_id, topic_name)
|
||||
topic = publisher.create_topic(request={"name": topic_path})
|
||||
topic = publisher.create_topic(request={"name": topic_path}, timeout=30)
|
||||
|
||||
|
||||
# Create a Pub/Sub subscription
|
||||
@@ -38,6 +38,7 @@ subscription = subscriber.create_subscription(
|
||||
name=subscription_path, topic=topic_path
|
||||
)
|
||||
|
||||
'''
|
||||
# Publish a message to the topic
|
||||
# Read the data from the csv file
|
||||
file_path = "C:/Users/Karen/Desktop/NFL_Cities.csv"
|
||||
@@ -59,3 +60,4 @@ async def receive_messages():
|
||||
|
||||
|
||||
asyncio.run(receive_messages())
|
||||
'''
|
||||
@@ -7,7 +7,7 @@ import time
|
||||
import os
|
||||
from concurrent.futures import TimeoutError
|
||||
|
||||
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "C:/Users/Karen/AppData/Roaming/gcloud/application_default_credentials.json"
|
||||
# os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "C:/Users/Karen/AppData/Roaming/gcloud/application_default_credentials.json"
|
||||
|
||||
# Configure the Selenium driver options
|
||||
options = Options()
|
||||
@@ -15,7 +15,8 @@ options.add_argument('--disable-gpu')
|
||||
driver = webdriver.Chrome(options=options)
|
||||
|
||||
# Set up Pub/Sub subscription
|
||||
project_id = "fp-20230425"
|
||||
# project_id = "fp-20230425"
|
||||
project_id = "svig-384801"
|
||||
subscription_name = "fp-subscription-zipcodes"
|
||||
subscriber = pubsub_v1.SubscriberClient()
|
||||
subscription_path = subscriber.subscription_path(project_id, subscription_name)
|
||||
@@ -24,7 +25,7 @@ timeout = 5
|
||||
# Load the product page and extract the __NEXT_DATA__ data
|
||||
url = 'https://www.sayweee.com/en/product/Want-Want-Senbei-Rice-Crackers/17119'
|
||||
driver.get(url)
|
||||
time.sleep(5)
|
||||
# time.sleep(5)
|
||||
'''
|
||||
next_data_element = driver.find_element(By.XPATH, "//script[@type='application/ld+json']")
|
||||
if next_data_element is not None:
|
||||
@@ -43,16 +44,30 @@ def callback(message):
|
||||
zip_code = message.data.decode('utf-8')
|
||||
print(f"Received zip code: {zip_code}")
|
||||
|
||||
# Enter zip code in the input field and click the "Check" button
|
||||
zip_input = driver.find_element(By.XPATH, '/html/body/div[2]/div/header/div[3]/div/div/div[2]/div[1]/div/div/div[1]')
|
||||
zip_input.clear()
|
||||
zip_input.send_keys(zip_code)
|
||||
check_button = driver.find_element(By.XPATH, '/html/body/div[5]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div/div/div[1]/div/div/div[3]/div')
|
||||
check_button.click()
|
||||
time.sleep(2)
|
||||
|
||||
# Extract the price of rice crackers for this zip code
|
||||
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)
|
||||
|
||||
# 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]/div/div/div[1]')
|
||||
print(zip_button)
|
||||
zip_button.click()
|
||||
time.sleep(1)
|
||||
|
||||
zip_input = driver.find_element(By.XPATH, '//div/label/following-sibling::input')
|
||||
zip_input.clear()
|
||||
zip_input.send_keys(zip_code)
|
||||
|
||||
check_button = driver.find_element(By.XPATH, "//button[text() = 'Confirm']")
|
||||
check_button.click()
|
||||
time.sleep(2)
|
||||
|
||||
# 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:]
|
||||
@@ -67,10 +82,10 @@ def callback(message):
|
||||
price = next_data['offers']['price']
|
||||
# Print the price
|
||||
print(price)
|
||||
except:
|
||||
price = 'Price not found'
|
||||
finally:
|
||||
message.ack() # acknowledge receipt of message
|
||||
except:
|
||||
print(f"Price not found for zip code {zip_code}")
|
||||
price = 'Price not found'
|
||||
|
||||
# Start receiving messages
|
||||
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
|
||||
|
||||
Reference in New Issue
Block a user