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/