【问题标题】:Errors while scraping this site using Python [closed]使用 Python 抓取此站点时出错 [关闭]
【发布时间】:2012-05-28 02:13:04
【问题描述】:

我无法从this weather website 获取一些信息。我主要是试图检索“当前状况”信息表。 这是我目前所拥有的,但它给了我一些错误。

import urllib2
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen('http://www.timeanddate.com/weather/usa/los-angeles').read())

for row in soup('table', {'class' : 'rpad'})[0].tbody('trtd'):
tds = row('tr')
print tds.string

【问题讨论】:

  • 您能否提供您收到的错误信息,包括回溯信息?
  • Traceback(最近一次调用最后一次):文件“C:\Users\Andrew\Desktop\test.py”,第 8 行,在 中 for row in soup('table', {' class' : 'rpad'})[0].tbody('trtd'): TypeError: 'NoneType' object is not callable
  • 我不确定自己做错了什么

标签: python web-scraping


【解决方案1】:
#!/usr/bin/env python
import urllib2
from bs4 import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen('http://www.timeanddate.com/weather/usa/los-angeles').read())

for tr in soup.table.find_all('tr'):
    if tr('th'):
        if tr.th.string == "Current conditions":
            tds = tr('td')
            for td in tds:
                print td.string

我的解决方案,运行时的输出如下所示:

None
Location:
Los Angeles / USC Campus Downtown
Temperature:
63 °F
Comfort Level:
63 °F
Dew point:
51 °F
Pressure:
29.94 "Hg
Humidity:
65%
Visibility:
10 mi
Wind:
No wind
Last update:
Sun 7:47 PM PDT

因为我累了,所以没有花额外的时间为第一个输出添加无检查。也没有很明显的格式,但是你自己添加也不会太难。

希望对你有所帮助。

【讨论】:

  • 如果您不介意通过单击上下投票箭头下方的复选标记来接受我的回答,我将不胜感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-22
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2018-06-22
相关资源
最近更新 更多