75 lines
2.8 KiB
Python
75 lines
2.8 KiB
Python
from google.cloud import pubsub_v1
|
|||
|
|
from selenium import webdriver
|
||
|
|
from selenium.webdriver.chrome.options import Options
|
||
|
|
from selenium.webdriver.common.by import By
|
||
|
|
import json
|
||
|
|
import time
|
||
|
|
import os
|
||
|
|
|
||
|
|
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "C:/Users/Karen/AppData/Roaming/gcloud/application_default_credentials.json"
|
||
|
|
|
||
|
|
# Configure the Selenium driver options
|
||
|
|
options = Options()
|
||
|
|
options.add_argument('--disable-gpu')
|
||
|
|
driver = webdriver.Chrome(options=options)
|
||
|
|
|
||
|
|
# Set up Pub/Sub subscription
|
||
|
|
project_id = "fp-20230425"
|
||
|
|
subscription_name = "fp-subscription-zipcodes"
|
||
|
|
subscriber = pubsub_v1.SubscriberClient()
|
||
|
|
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'
|
||
|
|
driver.get(url)
|
||
|
|
time.sleep(5)
|
||
|
|
next_data_element = driver.find_element(By.XPATH, "//script[@type='application/ld+json']")
|
||
|
|
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)
|
||
|
|
price = next_data['offers']['price']
|
||
|
|
else:
|
||
|
|
print('__NEXT_DATA__ element not found')
|
||
|
|
|
||
|
|
# Define callback function for Pub/Sub messages
|
||
|
|
def callback(message):
|
||
|
|
# Extract zip code from message data
|
||
|
|
zip_code = message.data.decode('utf-8').strip()
|
||
|
|
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[4]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div/div/div[1]/div/div/div[2]/input')
|
||
|
|
zip_input.clear()
|
||
|
|
zip_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')
|
||
|
|
check_button.click()
|
||
|
|
time.sleep(2)
|
||
|
|
'''
|
||
|
|
# Extract the price of rice crackers for this zip code
|
||
|
|
try:
|
||
|
|
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:]
|
||
|
|
except:
|
||
|
|
price = 'Price not found'
|
||
|
|
'''
|
||
|
|
print(next_data)
|
||
|
|
price = next_data['offers']['price']
|
||
|
|
# Print the price
|
||
|
|
print(price)
|
||
|
|
|
||
|
|
# Print the zip code and its corresponding price
|
||
|
|
print(f'{zip_code}: {price}')
|
||
|
|
|
||
|
|
message.ack() # acknowledge receipt of message
|
||
|
|
|
||
|
|
# Start receiving messages
|
||
|
|
subscriber.subscribe(subscription_path, callback=callback)
|
||
|
|
print(f'Started receiving messages from subscription {subscription_path}...')
|
||
|
|
|
||
|
|
# Keep the script running indefinitely to continue receiving messages
|
||
|
|
while True:
|
||
|
|
time.sleep(10)
|