forked from SVIG/python-bootcamp
modified fp2_9
This commit is contained in:
@@ -25,6 +25,7 @@ timeout = 5
|
|||||||
url = 'https://www.sayweee.com/en/product/Want-Want-Senbei-Rice-Crackers/17119'
|
url = 'https://www.sayweee.com/en/product/Want-Want-Senbei-Rice-Crackers/17119'
|
||||||
driver.get(url)
|
driver.get(url)
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
'''
|
||||||
next_data_element = driver.find_element(By.XPATH, "//script[@type='application/ld+json']")
|
next_data_element = driver.find_element(By.XPATH, "//script[@type='application/ld+json']")
|
||||||
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')
|
||||||
@@ -34,6 +35,7 @@ if next_data_element is not None:
|
|||||||
next_data = json.loads(next_data_json)
|
next_data = json.loads(next_data_json)
|
||||||
else:
|
else:
|
||||||
print('__NEXT_DATA__ element not found')
|
print('__NEXT_DATA__ element not found')
|
||||||
|
'''
|
||||||
|
|
||||||
# Define callback function for Pub/Sub messages
|
# Define callback function for Pub/Sub messages
|
||||||
def callback(message):
|
def callback(message):
|
||||||
@@ -42,18 +44,29 @@ def callback(message):
|
|||||||
print(f"Received zip code: {zip_code}")
|
print(f"Received zip code: {zip_code}")
|
||||||
|
|
||||||
# Enter zip code in the input field and click the "Check" button
|
# 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 = 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.clear()
|
||||||
zip_input.send_keys(zip_code)
|
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 = 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()
|
check_button.click()
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
||||||
# Extract the price of rice crackers for this zip code
|
# Extract the price of rice crackers for this zip code
|
||||||
try:
|
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_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:]
|
price = price_element.text.strip()[1:]
|
||||||
print(price)
|
print(price)
|
||||||
|
'''
|
||||||
|
next_data_element = driver.find_element(By.XPATH, "//script[@type='application/ld+json']")
|
||||||
|
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']
|
||||||
|
# Print the price
|
||||||
|
print(price)
|
||||||
except:
|
except:
|
||||||
price = 'Price not found'
|
price = 'Price not found'
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
Reference in New Issue
Block a user