我正在编写一个脚本来从网页上收集天气数据。我的代码如下:
import urllib.request
from bs4 import BeautifulSoup
# open the webpage and assign the content to a new variable
base = urllib.request.urlopen('http://www.weather.com/weather/today/Washington+DC+20006:4:US')
f = base.readlines()
f = str(f)
soup = BeautifulSoup(f)
rn_base = soup.find(itemprop="temperature-fahrenheit")
right_now = rn_base.string
print(right_now)
fl_base = soup.find(itemprop="feels-like-temperature-fahrenheit")
feels_like = fl_base.string
print(feels_like)
td_base = soup.find_all('class_="wx-temperature"')
print(td_base)
因此
right_now
和feels_like
可以正常打印,但是当涉及td_base
时,它返回None
或[]
,这是一个空列表,具体取决于使用.find
还是.find_all
。为了解释HTML源代码,我的代码能够找到itemprop="temperature-fahrenheit"
和itemprop="feels-like-temperature-fahrenheit"
,但是在class_="wx-temperature"
上失败。我很高兴有任何关于为什么前两个成功的想法,而不是第三个成功的想法。谢谢!附言:以下是与手头任务相关的html源代码的摘录:
<div class="wx-data-part wx-first">
<div class="wx-temperature"><span itemprop="temperature-fahrenheit">87</span><span class="wx-degrees">°<span class="wx-unit">F</span></span></div>
<div class="wx-temperature-label">FEELS LIKE
<span itemprop="feels-like-temperature-fahrenheit">93</span>°</div>
</div>
<div class="wx-data-part">
<div class="wx-temperature">94<span class="wx-degrees">°</span></div>
<div class="wx-temperature-label">HIGH AT 3:25 PM</div>
</div>
<div class="wx-data-part">
<div class="wx-temperature">76<span class="wx-degrees">°</span></div>
<div class="wx-temperature-label">LOW</div>
</div>
最佳答案
删除周围的'
:
td_base = soup.find_all(class_="wx-temperature")