匹配html中的确切类 tags using BeautifulSoup

问题描述 投票:3回答:2

我正在使用Beautiful Soup从网站上抓取信息。

相关代码:

page_url = https://www.autotrader.co.uk/car-search?sort=sponsored&radius=1500&postcode=&onesearchad=Used&onesearchad=Nearly%20New&onesearchad=New&make=Vauxhall&model=Corsa&year-from=2008&year-to=2010&minimum-mileage=82376&maximum-mileage=123564&page=2

page = urllib2.urlopen(page_url)

soup = BeautifulSoup(page, 'html.parser')

现在我只想打印<div class="vehicle-price"></div>标签内页面上的每个价格,例如:

<div class="vehicle-price" data-label="search appearance click">\xa34,400</div>

所以我使用:

for i in soup.select('div.vehicle-price'):
    print (i.string)

这工作正常除了有一些像这样的<div>标签:

<div class="vehicle-price physical-stock-mrrp" data-label="search 
appearance click new car">

代码仍会打印这些标签中的内容。

我怎么能告诉Beautiful Soup我只想要class="vehicle-price"时的标签内容而不是class="vehicle-price other-things-too"

web-scraping beautifulsoup
2个回答
3
投票

您可以使用:not() CSS pseudo-class排除其他类

.vehicle-price:not(.physical-stock-mrrp)

BeautifulSoup 4.7.1

例如,您可以使用Or语法进行链接。示例链接将是.vehicle-price:not(.physical-stock-mrrp), .vehicle-price:not(.somethingElse)。其他选择器的想法可能包括传递attribute = value选择器并使用^,*,$运算符来指定要在属性值中匹配的子字符串。显然,感谢@facelessuser,您还可以将选择器列表传递给:not


3
投票

你可以use a custom function匹配所有div只有vehicle-price类。

html="""
<div class="vehicle-price" data-label="search appearance click">\xa34,400</div>
<div class="vehicle-price physical-stock-mrrp" data-label="search
appearance click new car">
</div>
"""
from bs4 import BeautifulSoup,Tag
import re
soup=BeautifulSoup(html,'lxml')
def my_match_function(elem):
 if isinstance(elem,Tag) and elem.name=='div' and ''.join(elem.attrs['class'])=='vehicle-price':
     return True
print(soup.find_all(my_match_function))

产量

[<div class="vehicle-price" data-label="search appearance click">£4,400</div>]
© www.soinside.com 2019 - 2024. All rights reserved.