Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在beautifulsoup中使用findAll筛选结果_Python_Search_Beautifulsoup_Findall - Fatal编程技术网

Python 在beautifulsoup中使用findAll筛选结果

Python 在beautifulsoup中使用findAll筛选结果,python,search,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 so

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

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"})
仅获取第一个
在本例中还有8个需要收集


所以希望从介绍开始到故事结尾都能收集到。。。有什么想法吗

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

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

对于CSS选择器,您希望选择
中的所有
p
元素。故事正文

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

。你没有用漂亮的汤4,是吗?不,在这个例子中没有,我想我明天会继续,你应该这样做。对于新代码,没有什么理由不升级…您没有使用Beautiful Soup 4,是吗?在本例中没有,我想我明天会继续讨论,您应该这样做。对于新代码,没有什么理由不升级。