python - 使用 beautifulsoup 中的 findAll 过滤结果

标签 python search beautifulsoup findall

import urllib2
from BeautifulSoup import BeautifulSoup

result = urllib2.urlopen("http://www.bbc.co.uk/news/uk-scotland-south-scotland-12380537")
html=result.read()
soup= BeautifulSoup(html)
print soup.html.head.title

print soup.findAll('div', attrs={ "class" : "story-body"})

问题似乎是我想要的信息在故事正文中,但它位于最底部。所以我最终会收到大量垃圾信息,直到我到达那里。

print soup.findAll('p', attrs={ 'class' : "introduction"})

只给我第一个<p>本例中还有 8 个需要收集

所以希望收集从介绍开始到故事主体结束的内容......有什么想法吗?

最佳答案

就 CSS 选择器而言,您想要选择 .story-body 中的所有 p 元素:

print soup.select('.story-body p')

http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.html?highlight=select#css-selectors

关于python - 使用 beautifulsoup 中的 findAll 过滤结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10522091/

相关文章:

python - opencv/ python : motion detect weird thresholding

python - Django Forms.py 电子邮件和电话验证

python - 计算 Pandas 系列中相同符号的累积值和顺序值

python - 快速从另一个数据帧创建 pandas 数据帧

python - 使用 Beautifulsoup 进行数据抓取。找错 body

python - 如何使用 python,BeautifulSoup 获取跨度值

html - 从 HTML 到文本的 NLP 预处理

algorithm - 确定树遍历是广度优先、深度优先还是两者都不是

javascript - 网站上有谷歌搜索吗?

php - 全文搜索是答案吗?