我是一个新的Python。我想得到如下代码的结果:
Score Postive Negative
5 good bad
7 interesting
3 horrible
但是我的代码输出没什么。求你了问题出在哪里?你知道吗
from bs4 import BeautifulSoup
text = """
... <body>
<div class="review">
<p class="pos">good</p>
<p class="neg">bad</p>
</div>
<div class="review">
<p class="pos">interesting</p>
</div>
<div class="review">
<p class="neg">horrible</p>
</div>
... </body>"""
soup = BeautifulSoup(text)
for parent in soup.find_all('div', attrs={'class': 'review'}):
if parent.findNextSiblings('p', attrs={'class': 'pos'}):
postive.append(parent.get_text())
else:
postive.append("")
if parent.findNextSiblings('p', attrs={'class': 'neg'}):
negtive.append(parent.get_text())
else:
negtive.append("")
p
标记不是类review
的div
标记的同级,它们是子级:印刷品:
相关问题 更多 >
编程相关推荐