我正在尝试抓取本网站的“listing-key-specs”:https://www.autotrader.co.uk/car-search?radius=30&postcode=ss156ee&onesearchad=Used&make=Renault&model=zoe&page=1但我只对里程规格感兴趣,而不对 bhp 或任何其他规格感兴趣。如果我输入specs=article.find('ul',class_="listing-key-specs")print(specs.text)我可能会得到6条信息:2015 (65 reg)Hatchback13,033 miles88bhpAutomaticElectric**如果我输入print(specs.li.text)我只会得到第一个规格,即2015 (65 reg)如何选择特定的规格?让我们说“英里”规格?
2 回答
江户川乱折腾
TA贡献1851条经验 获得超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
提交
取消
