This fixes issue #1 #2
@@ -1,5 +1,6 @@
|
|||||||
from selenium import webdriver
|
from selenium import webdriver
|
||||||
from selenium.webdriver.chrome.options import Options
|
from selenium.webdriver.chrome.options import Options
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
import json
|
import json
|
||||||
|
|
||||||
# Configure the Selenium driver options
|
# Configure the Selenium driver options
|
||||||
@@ -9,13 +10,13 @@ options.add_argument('--disable-gpu')
|
|||||||
driver = webdriver.Chrome(options=options)
|
driver = webdriver.Chrome(options=options)
|
||||||
|
|
||||||
# Load the page and extract the __NEXT_DATA__ data
|
# Load the page and extract the __NEXT_DATA__ data
|
||||||
url = 'https://www.sayweee.com/en/product/Pete-and-Gerry-s-Organic-Large-Brown-Eggs/65902?category=dairy02'
|
url = 'https://www.sayweee.com/en/product/Want-Want-Senbei-Rice-Crackers/17119'
|
||||||
driver.get(url)
|
driver.get(url)
|
||||||
next_data_element = None
|
next_data_element = None
|
||||||
for script_element in driver.find_elements_by_xpath('//script'):
|
try:
|
||||||
if '__NEXT_DATA__' in script_element.get_attribute('innerHTML'):
|
next_data_element = driver.find_element(By.XPATH, '//script[contains(text(), "__NEXT_DATA__")]')
|
||||||
next_data_element = script_element
|
except:
|
||||||
break
|
print('__NEXT_DATA__ element not found')
|
||||||
|
|
||||||
if next_data_element is not None:
|
if next_data_element is not None:
|
||||||
next_data_text = next_data_element.get_attribute('innerHTML')
|
next_data_text = next_data_element.get_attribute('innerHTML')
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import re
|
||||||
|
import json
|
||||||
|
from selenium import webdriver
|
||||||
|
from selenium.webdriver.chrome.options import Options
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
|
|
||||||
|
# Configure the Selenium driver options
|
||||||
|
options = Options()
|
||||||
|
options.add_argument('--headless')
|
||||||
|
options.add_argument('--disable-gpu')
|
||||||
|
driver = webdriver.Chrome(options=options)
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
next_data_element = None
|
||||||
|
for script_element in driver.find_elements(By.XPATH, '//script'):
|
||||||
|
script_text = script_element.get_attribute('innerHTML')
|
||||||
|
match = re.search(r'__NEXT_DATA__ = (\{.*\});', script_text)
|
||||||
|
if match:
|
||||||
|
next_data_json = match.group(1)
|
||||||
|
next_data = json.loads(next_data_json)
|
||||||
|
# Extract the price from the __NEXT_DATA__ data
|
||||||
|
price = next_data['props']['pageProps']['product']['price']
|
||||||
|
# Print the price
|
||||||
|
print(price)
|
||||||
|
break
|
||||||
|
|
||||||
|
if next_data_element is None:
|
||||||
|
print('__NEXT_DATA__ element not found')
|
||||||
|
|
||||||
|
# Close the Selenium driver
|
||||||
|
driver.quit()
|
||||||
Reference in New Issue
Block a user