53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
from selenium import webdriver
|
|||
|
|
from selenium.webdriver.chrome.options import Options
|
||
|
|
from selenium.webdriver.common.by import By
|
||
|
|
import json
|
||
|
|
import time
|
||
|
|
|
||
|
|
# Configure the Selenium driver options
|
||
|
|
options = Options()
|
||
|
|
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']
|
||
|
|
|
||
|
|
# Load the 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)
|
||
|
|
|
||
|
|
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
|
||
|
|
try:
|
||
|
|
next_data_element = driver.find_element(By.XPATH, "//script[@type='application/ld+json']")
|
||
|
|
except:
|
||
|
|
print('__NEXT_DATA__ element not found')
|
||
|
|
|
||
|
|
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']
|
||
|
|
|
||
|
|
# Print the price for the current zip code
|
||
|
|
print(f"Price for zip code {zip_code}: {price}")
|
||
|
|
else:
|
||
|
|
print(f"Price not found for zip code {zip_code}")
|
||
|
|
|
||
|
|
# Close the Selenium driver
|
||
|
|
driver.quit()
|