gpt4 book ai didi

python - Nonetype 对象不可调用

转载 作者:太空宇宙 更新时间:2023-11-04 07:44:06 24 4
gpt4 key购买 nike

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 object not callable”

最佳答案

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')

关于python - Nonetype 对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12067156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com