我正在编写一个脚本来从网页收集天气数据。我的代码如下:
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
or []
,一个空列表取决于是否使用.find
or .find_all
。关于 HTML 源代码的解释,我的代码能够找到itemprop="temperature-fahrenheit"
and itemprop="feels-like-temperature-fahrenheit"
,但在class_="wx-temperature"
. 对于为什么前两个会成功但第三个不会成功的任何想法,我将不胜感激。谢谢!
PS:这是与手头任务相关(IMO)的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>