[英]Beautiful soup returns none data
我是 web 的新手,抓取soup.find()
找不到它返回的數據,這里沒有我想找到價格我的代碼是
destination = "nepal"
check_in_year = 2022
check_in_month = 12
check_in_day = 13
check_out_year = 2022
check_out_month = 12
check_out_day = 17
adults = 2
total_children = 0
num_rooms = 1
url = "https://www.booking.com/searchresults.en-gb.html?label=gen173nr-1BCAEoggI46AdIM1gEaFCIAQGYAQm4ARfIAQzYAQHoAQGIAgGoAgO4AqnamocGwAIB0gIkNGEyODNlYTYtYTM2Yi00M2Y3LWE2YjItM2RmYWFlMTM5ZWI22AIF4AIB&sid=44053b754f64b58cfdde1ddc395974a0&sb=1&sb_lp=1&src=index&src_elem=sb&error_url=https%3A%2F%2Fwww.booking.com%2Findex.en-gb.html%3Flabel%3Dgen173nr-1BCAEoggI46AdIM1gEaFCIAQGYAQm4ARfIAQzYAQHoAQGIAgGoAgO4AqnamocGwAIB0gIkNGEyODNlYTYtYTM2Yi00M2Y3LWE2YjItM2RmYWFlMTM5ZWI22AIF4AIB%3Bsid%3D44053b754f64b58cfdde1ddc395974a0%3Bsb_price_type%3Dtotal%26%3B&ss={}&is_ski_area=0&checkin_year={}&checkin_month={}&checkin_monthday={}&checkout_year={}&checkout_month={}&checkout_monthday={}&group_adults={}&{}&no_rooms={}&b_h4u_keep_filters=&from_sf=1&dest_id=&dest_type=&search_pageview_id=1be740bf37ad0063&search_selected=false".format(
destination,check_in_year,check_in_month,check_in_day,check_out_year,check_out_month,check_out_day,adults,total_children,num_rooms)
print(url)
它打印出 url 正常,並且訪問網站時的結果與我要搜索的結果相同,但這是主要內容
driver.get(url)
soup = BeautifulSoup(driver.page_source)
links = soup.find("span", {"data-testid":"price-and-discounted-price"})
print(links)
它打印出None
請幫我解決這個問題我想找到價格。
無法重現所有請求的行為,但有時網站上沒有顯示折扣價,當然可能會發生什么,因為該網站也在測試和交付基於行為或 header 信息(如user-agent
或device
)的內容.
另一個情況可能是你會遇到一個捕獲器,所以你應該檢查你的driver.page_source
各自的soup
。
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
from bs4 import BeautifulSoup
destination = "nepal"
check_in_year = 2022
check_in_month = 12
check_in_day = 13
check_out_year = 2022
check_out_month = 12
check_out_day = 17
adults = 2
total_children = 0
num_rooms = 1
url = "https://www.booking.com/searchresults.en-gb.html?label=gen173nr-1BCAEoggI46AdIM1gEaFCIAQGYAQm4ARfIAQzYAQHoAQGIAgGoAgO4AqnamocGwAIB0gIkNGEyODNlYTYtYTM2Yi00M2Y3LWE2YjItM2RmYWFlMTM5ZWI22AIF4AIB&sid=44053b754f64b58cfdde1ddc395974a0&sb=1&sb_lp=1&src=index&src_elem=sb&error_url=https%3A%2F%2Fwww.booking.com%2Findex.en-gb.html%3Flabel%3Dgen173nr-1BCAEoggI46AdIM1gEaFCIAQGYAQm4ARfIAQzYAQHoAQGIAgGoAgO4AqnamocGwAIB0gIkNGEyODNlYTYtYTM2Yi00M2Y3LWE2YjItM2RmYWFlMTM5ZWI22AIF4AIB%3Bsid%3D44053b754f64b58cfdde1ddc395974a0%3Bsb_price_type%3Dtotal%26%3B&ss={}&is_ski_area=0&checkin_year={}&checkin_month={}&checkin_monthday={}&checkout_year={}&checkout_month={}&checkout_monthday={}&group_adults={}&{}&no_rooms={}&b_h4u_keep_filters=&from_sf=1&dest_id=&dest_type=&search_pageview_id=1be740bf37ad0063&search_selected=false".format(
destination,check_in_year,check_in_month,check_in_day,check_out_year,check_out_month,check_out_day,adults,total_children,num_rooms)
# url = 'https://www.amazon.co.uk/s?k=ultrawide+monitor&crid=R4VD48NS2JN6&sprefix=ultrawide+monitor%2Caps%2C177&ref=nb_sb_noss_1'
driver.get(url)
soup = BeautifulSoup(driver.page_source)
找到的第一個結果:
print(soup.find("span", {"data-testid":"price-and-discounted-price"}))
-> <span aria-hidden="true" class="fcab3ed991 fbd1d3018c e729ed5ab6" data-testid="price-and-discounted-price">€ 42</span>
ResultSet
中所有可用的選擇器為css selectors
:
print(soup.select('span[data-testid="price-and-discounted-price"]'))
-> [<span aria-hidden="true" class="fcab3ed991 fbd1d3018c e729ed5ab6" data-testid="price-and-discounted-price">€ 42</span>,
<span aria-hidden="true" class="fcab3ed991 fbd1d3018c e729ed5ab6" data-testid="price-and-discounted-price">€ 183</span>,
<span aria-hidden="true" class="fcab3ed991 fbd1d3018c e729ed5ab6" data-testid="price-and-discounted-price">€ 28</span>,...]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.