使用BeautifulSoup打印具有<div>标记</div>的特定信息

时间:2015-03-13 23:36:54

标签: python-2.7 printing beautifulsoup

我仍然不习惯使用BeautifulSoup来抓取网站上的信息。对于这段代码,我特意试图抓住这个值,而其他人喜欢它,并以更简洁易读的显示方式将其显示给用户。下面是我使用突出显示的div和我正在尝试解析的类的截图:

enter image description here

这是我正在使用的代码:

import urllib2
from bs4 import BeautifulSoup
a =("http://forecast.weather.gov/MapClick.php?lat=39.32196712788175&lon=-82.10190859830237&site=all&smap=1#.VQM_kOGGP7l")
website = urllib2.urlopen(a)
html = website.read()
soup = BeautifulSoup(html)
x = soup.find_all("div",{"class": "point-forecast-icons-low"})
print x

然而,一旦它运行它返回“[]”我没有错误但没有任何反应。我一开始的想法可能是它在我告诉它搜索的<div>内找不到任何东西,但通常我会从代码中找回一条没有找到任何内容的东西。所以我认为现在使用我的代码可能是因为它不是打开div来从内部拉出其他内容,但这只是我最好的猜测。

1 个答案:

答案 0 :(得分:0)

您收到[]因为point-forecast-icons-low类不是div的属性,而是p标记的属性。试试这个。

x = soup.find_all("p", attrs={"class": "point-forecast-icons-low"})