Nonetype对象不可调用

时间:2012-08-22 06:04:40

标签: python beautifulsoup

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对象不可调用”

2 个答案:

答案 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&nbsp;°C</td></tr><tr><td>Comfort Level:</td><td>26&nbsp;°C</td></tr><tr><td>Dew point:</td><td>21&nbsp;°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&deg; 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)

如果您看到网址的页面来源

  

http://www.timeanddate.com/weather/usa/tucson

,类table的{​​{1}}元素没有 一个rpad子元素。

tbody

您需要提取数据,并牢记这一结构。直接遍历<table class=rpad><tr><td>Location:</td><td>Davis-Monthan Air Force Base</td></tr><tr><td>Temperature:....tr元素。