Files

44 lines
1.3 KiB
Python
Raw Permalink Normal View History

2023-04-26 19:14:54 -04:00
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
2023-04-28 19:30:00 -04:00
from selenium.webdriver.common.by import By
2023-04-26 19:14:54 -04:00
import json
2023-05-01 12:52:50 -04:00
import time
2023-04-26 19:14:54 -04:00
# Configure the Selenium driver options
options = Options()
2023-05-01 12:52:50 -04:00
#options.add_argument('--headless')
2023-04-26 19:14:54 -04:00
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=options)
# Load the page and extract the __NEXT_DATA__ data
2023-04-28 19:30:00 -04:00
url = 'https://www.sayweee.com/en/product/Want-Want-Senbei-Rice-Crackers/17119'
2023-04-26 19:14:54 -04:00
driver.get(url)
2023-05-01 12:52:50 -04:00
time.sleep(5)
2023-04-26 19:14:54 -04:00
next_data_element = None
2023-05-01 12:52:50 -04:00
#try:
next_data_element = driver.find_element(By.XPATH, "//script[@type='application/ld+json']")
#except:
# print('__NEXT_DATA__ element not found')
2023-04-26 19:14:54 -04:00
2023-05-01 12:52:50 -04:00
#time.sleep(1000)
2023-04-26 19:14:54 -04:00
if next_data_element is not None:
next_data_text = next_data_element.get_attribute('innerHTML')
2023-05-01 12:52:50 -04:00
print(next_data_text)
2023-04-26 19:14:54 -04:00
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
2023-05-01 12:52:50 -04:00
# price = next_data['props']['pageProps']['product']['price']
print(next_data)
price = next_data['offers']['price']
2023-04-26 19:14:54 -04:00
# Print the price
print(price)
else:
print('__NEXT_DATA__ element not found')
2023-05-01 12:52:50 -04:00
time.sleep(1000)
2023-04-26 19:14:54 -04:00
# Close the Selenium driver
2023-05-01 12:52:50 -04:00
#driver.quit()