在HTML解析器中选择含有附加词的类名。

问题描述 投票:0回答:1

我正试图搜刮一个网页。我想获得评论。但评论分为三类,有些是正面的,有些是中性的,有些是负面的。我使用的是html解析器,也访问了很多标签。但是对于可以分为三类的类,我怎么能得到它们。

<div class="review positive" title="" style="background-color: #00B551;">9.3</div>
<div class="review negative" title="" style="background-color: #FF0000;">4.8</div>
<div class="review neutral" title="" style="background-color: #FFFF00;">6</div>

我有一个Python容器,每个div都包含了每个项目。

# finds each product from the store page
containers = page_soup.findAll("div", {"class": "item-container"})`

for container in containers:
    title = container.findAll(a).text #This gives me titles
    ##Similarly I need the reviews of each of them here
    review = container.findAll("div", {"class": "review "}))#along with review there is positive, neutral and negative word also according to the type of review
web-scraping beautifulsoup html-parsing
1个回答
0
投票

使用regex,你可以得到包含子串的类。"review".

import re

for container in containers:
    title = container.findAll(a).text #This gives me titles

    review = container.findAll("div", {"class": re.compile(r'review')})

看到的区别。

html = '''<div class="review positive" title="" style="background-color: #00B551;">9.3</div>
<div class="review negative" title="" style="background-color: #FF0000;">4.8</div>
<div class="review neutral" title="" style="background-color: #FFFF00;">6</div>'''

from bs4 import BeautifulSoup
import re

soup = BeautifulSoup(html, 'html.parser')
review = soup.find_all('div', {'class':'review '})
print ('No regex: ',review)

print('\n')

review = soup.findAll("div", {"class": re.compile(r'review')})
print ('Regex: ',review)

输出。

No regex:  []


Regex:  [<div class="review positive" style="background-color: #00B551;" title="">9.3</div>, <div class="review negative" style="background-color: #FF0000;" title="">4.8</div>, <div class="review neutral" style="background-color: #FFFF00;" title="">6</div>]
© www.soinside.com 2019 - 2024. All rights reserved.