import urllib2
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen('http://www.timeanddate.com/weather/usa/tucson).read())
for row in soup('table', {'class' : 'rpad'})[0].tbody('tr'):
tds = row('td')
print tds[0].string, tds[1].string
执行
时收到错误“Nonetype对象不可调用”答案 0 :(得分:4)
import urllib2
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen('http://www.timeanddate.com/weather/usa/tucson').read())
>>> print soup('table', {'class' : 'rpad'})[0]
<table class="rpad"><tr><td>Location:</td><td>Davis-Monthan Air Force Base</td></tr><tr><td>Temperature:</td><td>25 °C</td></tr><tr><td>Comfort Level:</td><td>26 °C</td></tr><tr><td>Dew point:</td><td>21 °C</td></tr><tr><td>Pressure:</td><td>1009 millibars</td></tr><tr><td>Humidity:</td><td>77%</td></tr><tr><td>Visibility:</td><td>16 km</td></tr><tr><td>Wind:</td><td>11 km/h from 280° West<img src="http://c.tadst.com/gfx/comp/sa8.png" width="14" height="14" alt="Direction East" title="Wind blowing from West to East" /></td></tr><tr><td>Last update:</td><td>Tue 9:55 PM MST</td></tr></table>
>>> 'tbody' in soup('table', {'class' : 'rpad'})[0]
False
>>> print soup('table', {'class' : 'rpad'})[0].tbody
None
None
无法调用,即您无法调用None('tr')
答案 1 :(得分:3)
如果您看到网址的页面来源
,类table
的{{1}}元素没有
一个rpad
子元素。
tbody
您需要提取数据,并牢记这一结构。直接遍历<table class=rpad><tr><td>Location:</td><td>Davis-Monthan Air Force Base</td></tr><tr><td>Temperature:....
和tr
元素。