![](/img/trans.png)
[英]How can I retrieve the price on airbnb using beautifulsoup to scrape?
[英]How can I scrape a product price from a website using Beautifulsoup?
我正在嘗試從 StockX 獲取這款運動鞋的最新出價,但我收到了錯誤消息
IndexError: list index out of range
因為由於某種原因, sneaker_price
出現空白。 有人可以幫忙嗎?:
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://stockx.com/air-jordan-6-retro-travis-scott")
soup = BeautifulSoup(driver.page_source,"lxml")
driver.quit()
sneaker_price = soup.select("div.en-us stat-value stat-small")[0]
試試 CSS 選擇器div.en-us.stat-value.stat-small
:
sneaker_price = soup.select("div.en-us.stat-value.stat-small")[0]
print(sneaker_price.text)
印刷:
€523
注意:如果您獲得驗證碼頁面,請嘗試指定更多 HTTP 標頭和/或 Cookie。 例如:
import requests
from bs4 import BeautifulSoup
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0',
'Accept-Language': 'en-US,en;q=0.5'
}
cookies = {
'stockx_homepage': "sneakers",
}
soup = BeautifulSoup(requests.get("https://stockx.com/air-jordan-6-retro-travis-scott", headers=headers, cookies=cookies).content,"lxml")
sneaker_price = soup.select("div.en-us.stat-value.stat-small")[0]
print(sneaker_price.text)
您可以使用.ask div.en-us.stat-value.stat-small
選擇器來獲取包含最新要價的 div。 由於有多個元素,您可以選擇最后一個元素,例如:
ask_price = soup.select('.ask div.en-us.stat-value.stat-small')[-1]
print(ask_price.text) # $655
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.