嗨,我正在嘗試從該站點https://turo.com/us/en/car-rental/united-states/austin-tx/tesla/model-s/1733237?endDate=11/09%抓取一些資料2F2022&endTime=11:00&startDate=11/05/2022&startTime=11:00使用劇作家,但我正在努力更改日期和時間。起初我試圖讓我的機器人點擊日期和時間框,但我一直遇到問題,所以我決定改變我的方法。
現在我正在嘗試更改 URL,以便機器人直接轉到我想要的日期,而不是轉到默認日期,但我還沒有找到讓它作業的方法,因為它一直將我重定向到默認頁面。
例子:
https://turo.com/us/en/car-rental/united-states/austin-tx/tesla/model-s/1733237?endDate=11/09/2022&endTime=11:00&startDate=11/05/2022&startTime= 11:00
此鏈接將您帶到開始日期 11/05/2022 和結束日期 11/09/2022 但想要更改它,以便通過更改 endDate=11/09/2022 將我帶到我決定的日期,而不是將我重定向回默認日期。
是否有另一種更容易更改日期的方法?
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context(user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36')
page = browser.new_page()
page.goto("https://turo.com/us/en/car-rental/united-states/austin-tx/tesla/model-s/1733237?endDate=11/10/2022&endTime=11:00&startDate=11/05/2022&startTime=11:00")
time.sleep(5)
html = page.inner_html('#__next')
soup = BeautifulSoup(html, 'html.parser')
uj5u.com熱心網友回復:
如我所見,無法使用 URL 執行此操作
無論如何,我準備了一個腳本來制作你想要的東西。我使用了您可以使用的輔助功能。我希望這個對你有用。
import time
from playwright.sync_api import Playwright, sync_playwright, expect
#This function wait for an element, if the element is not present it return false instead of throwing an exception and stopping the script. If element is not present after the timeout, it return false and we can use that false for our desired flow
def wait_for_selector_with_no_exception(page, selector, timeout):
try:
page.wait_for_selector(selector, timeout=timeout)
return True
except:
return False
with sync_playwright() as p:
#We define the browser, the context and the page
browser = p.chromium.launch(headless=False)
context = browser.new_context(
user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36')
page = browser.new_page()
#We navigate to URL of the car we want (In this case the Tesla from your example)
url_for_car = "https://turo.com/us/en/car-rental/united-states/austin-tx/tesla/model-s/1733237"
page.goto(url_for_car)
#We do click into start date dropdown element
page.locator("#reservationDates-dateRangePicker").click()
#Here we define the selector for the start date we want (You can replace it with your desired date, of course)
selector_for_start_date="//td[contains(@aria-label,'December 29, 2022')]"
#We use our auxiliar function to see if the start date is within the DOM, if not we click on arrow for moving to next month
while not wait_for_selector_with_no_exception(page=page, selector=selector_for_start_date, timeout=2000):
page.locator(".DayPickerNavigationNext_icon").click()
#Once desired start date is within the DOM we click on it
page.locator(selector_for_start_date).click()
#This selector is the second datepicker element once is open
selector_when_date_end_is_open = '//div[@]/..//input[@id="reservationDates-dateRangePicker-endDate"]'
#We wait till the second datepicker is already open
page.wait_for_selector(selector_when_date_end_is_open)
#Here we define the selector for the end date we want (Also you can replace it with your desired end date)
selector_for_end_date="//td[contains(@aria-label,'March 23, 2023')]"
#Same we did with start date but with end date, we wait for our end date element, if is not present we click on next arrow
while not wait_for_selector_with_no_exception(page=page, selector=selector_for_end_date, timeout=2000):
page.locator(".DayPickerNavigationNext_icon").click()
#Once our end date is present we click on it
page.locator(selector_for_end_date).click()
#This sleep you can remove it, is only for you to see that it worked as expected
time.sleep(10)
#We close the context and the browser
context.close()
browser.close()
如您所見,我使用 xpath 作為我的選擇器,因為它是我可以控制的,如果您找到一種使用 CSS 的方法,請繼續。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/520175.html
