我正在嘗試抓取本網(wǎng)站的“l(fā)isting-key-specs”:https://www.autotrader.co.uk/car-search?radius=30&postcode=ss156ee&onesearchad=Used&make=Renault&model=zoe&page=1但我只對里程規(guī)格感興趣,而不對 bhp 或任何其他規(guī)格感興趣。如果我輸入specs=article.find('ul',class_="listing-key-specs")print(specs.text)我可能會得到6條信息:2015 (65 reg)Hatchback13,033 miles88bhpAutomaticElectric**如果我輸入print(specs.li.text)我只會得到第一個規(guī)格,即2015 (65 reg)如何選擇特定的規(guī)格?讓我們說“英里”規(guī)格?
2 回答

江戶川亂折騰
TA貢獻1851條經(jīng)驗 獲得超5個贊
您可以提取第一個孩子 li
from bs4 import BeautifulSoup as bs
import requests
res= requests.get('https://www.autotrader.co.uk/car-search?radius=30&postcode=ss156ee&onesearchad=Used&make=Renault&model=zoe&page=1')
soup = bs(res.content, 'lxml')
details = [item.text for item in soup.select('.listing-key-specs li:first-child')]
print(details)
效率較低的是
.listing-key-specs li:nth-of-type(1)
或者
.listing-key-specs :nth-child(1)
或者
.listing-key-specs li:first-of-type
我正在使用最新的 BeautifulSoup 4.7.1
添加回答
舉報
0/150
提交
取消