Check the availability & time delay

This commit is contained in:
2023-05-09 19:02:36 -04:00
parent 8ed9dfbdf3
commit 7df52c95e1
2 changed files with 97 additions and 33 deletions
+5 -5
View File
@@ -32,7 +32,7 @@ driver.delete_all_cookies
driver.get(url) driver.get(url)
# time.sleep(5) # time.sleep(5)
time.sleep(5) time.sleep(2)
# Close the cookies setting window # Close the cookies setting window
try: try:
cookie_button = driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div[3]/button") cookie_button = driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div[3]/button")
@@ -41,13 +41,13 @@ try:
except: except:
pass pass
time.sleep(5) time.sleep(1)
# Clear any modal dialog # Clear any modal dialog
try: try:
modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]") modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]")
print(f"modal_close = {modal_close}") print(f"modal_close = {modal_close}")
modal_close.click() modal_close.click()
time.sleep(2) #time.sleep(2)
except: except:
pass pass
@@ -60,7 +60,7 @@ def callback(message):
try: try:
try: try:
time.sleep(5) time.sleep(2)
# Clear any modal dialog # Clear any modal dialog
modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]") modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]")
# print(f"modal_close = {modal_close}") # print(f"modal_close = {modal_close}")
@@ -100,7 +100,7 @@ def callback(message):
time.sleep(2) time.sleep(2)
try: try:
time.sleep(5) time.sleep(2)
# Clear any notice # Clear any notice
notice_close = driver.find_element(By.XPATH, "/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[1]/div[2]/button") notice_close = driver.find_element(By.XPATH, "/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[1]/div[2]/button")
print(f"notice_close = {notice_close}") print(f"notice_close = {notice_close}")
+91 -27
View File
@@ -10,43 +10,107 @@ options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=options) driver = webdriver.Chrome(options=options)
# Define the zip codes for which to extract the price # Define the zip codes for which to extract the price
zip_codes = ['10001', '90001', '90210'] zip_codes = ['85305', '30313', '21230', '14127','28202','60605','45202','44114','76011','80204','48226','54304','77054','46225','32202','64129','89118','90301','33056','55415','02035','70112','07073','19148','15212','95054','98134','33607','37213','20785']
# 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/Want-Want-Senbei-Rice-Crackers/17119' url = 'https://www.sayweee.com/en/product/Want-Want-Senbei-Rice-Crackers/17119'
#url = 'https://www.sayweee.com/en/product/Lays-Potato-Chips-Cucumber-Flavor/51261'
#url = 'https://www.sayweee.com/en/product/Glico-Pocky-Cookies-and-Cream-Family-Pack/79467'
#url = 'https://www.sayweee.com/en/product/Xiang-Na-Lan-Thai-Jasmine-Rice/19566?category=dried01'
#url = 'https://www.sayweee.com/en/product/Sekka-Premium-Japanese-Medium-Grain-Rice/81224'
#url = 'https://www.sayweee.com/en/product/Big-Green-Spring-Farm-Organic-Millet/90991?category=dried02'
#url = 'https://www.sayweee.com/en/product/Liuzhou-Guangxi-Specialty-LuoSiFen-Hot-Spicy-Noodles/95442?category=instant01'
#url = 'https://www.sayweee.com/en/product/Sanyangyishi-Fresh-Instant-Rice-Noodles/2156?category=instant01'
#url = 'https://www.sayweee.com/en/product/Nissin-Beef-Flavor-Cup-Noodles/102519?category=instant01'
#url = 'https://www.sayweee.com/en/product/Formosa-Pork-Sung-/23528?category=canned04'
driver.delete_all_cookies
driver.get(url) driver.get(url)
time.sleep(5) time.sleep(2)
for zip_code in zip_codes: # Close the cookies setting window
# 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: try:
next_data_element = driver.find_element(By.XPATH, "//script[@type='application/ld+json']") cookie_button = driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div[3]/button")
print(f"cookie_button = {cookie_button}")
cookie_button.click()
except: except:
print('__NEXT_DATA__ element not found') pass
if next_data_element is not None: time.sleep(5)
next_data_text = next_data_element.get_attribute('innerHTML') # Clear any modal dialog
next_data_start_index = next_data_text.index('{') try:
next_data_end_index = next_data_text.rindex('}') + 1 modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]")
next_data_json = next_data_text[next_data_start_index:next_data_end_index] print(f"modal_close = {modal_close}")
next_data = json.loads(next_data_json) modal_close.click()
time.sleep(2)
except:
pass
# Extract the price from the __NEXT_DATA__ data def callback(zip_code):
price = next_data['offers']['price'] try:
try:
time.sleep(5)
# Clear any modal dialog
modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]")
# print(f"modal_close = {modal_close}")
modal_close.click()
time.sleep(2)
except:
pass
# Print the price for the current zip code
print(f"Price for zip code {zip_code}: {price}") try:
else: time.sleep(1)
# Clear any notice
notice_close = driver.find_element(By.XPATH, "/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[1]/div[2]/button")
# print(f"notice_close = {notice_close}")
notice_close.click()
time.sleep(1)
except:
pass
# Enter zip code in the input field and click the "Check" button
zip_button = driver.find_element(By.XPATH, '/html/body/div[2]/div/header/div[3]/div/div/div[2]/div[1]')
# print(f"{zip_button:}")
zip_button.click()
time.sleep(1)
zip_input = driver.find_element(By.XPATH, '//div/label/following-sibling::input')
# print(f"{zip_input:}")
zip_input.click()
# print("clearing zip_input")
zip_input.send_keys("\b\b\b\b\b")
# print("sending zip code")
zip_input.send_keys(zip_code)
check_button = driver.find_element(By.XPATH, "//button[text() = 'Confirm']")
# print(f"{check_button:}")
check_button.click()
time.sleep(2)
try:
time.sleep(5)
# Clear any notice
notice_close = driver.find_element(By.XPATH, "/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[1]/div[2]/button")
print(f"notice_close = {notice_close}")
notice_close.click()
time.sleep(2)
except:
pass
# Extract the price of rice crackers for this zip code
try:
sold_out_notice = driver.find_element(By.XPATH, '//*[@id="soldStatusNotice"]')
print(f"No such product in this area = {sold_out_notice}")
notice_close.click()
except:
print(f"The product is available in this area {zip_code}")
except Exception as e:
print(f"Caught exception: {e}")
print(f"Price not found for zip code {zip_code}") print(f"Price not found for zip code {zip_code}")
# Close the Selenium driver for zip_code in zip_codes:
callback(zip_code)
driver.quit() driver.quit()