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个要收集
所以期待收集从介绍开始到故事结束......任何想法?
答案 0 :(得分:1)
就CSS选择器而言,您希望选择p
中的所有.story-body
元素:
print soup.select('.story-body p')
http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.html?highlight=select#css-selectors