使用python解析HTML中的表

2024-06-28 20:32:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试解析网页上的信息。基本上,我想从HTML中的表中提取一些信息,以便对其进行趋势分析。我一直关注的部分是解析表中的HTML

网页是http://weather.unbc.ca/wx/data-table.html

我试过使用:

import urllib2
from bs4 import BeautifulSoup


contenturl = "http://weather.unbc.ca/wx/data-table.html"


soup = BeautifulSoup(urllib2.urlopen(contenturl).read())

table = soup.find('tr', attrs={'class': 'content'})

rows = table.findAll('tr')
for tr in rows:
cols = tr.findAll('td')
if 'cell_c' in cols[0]['class']:
    # currency row
        Date_time, Record, Tair, Tdew, RH, pstn, pmsl, wspd_avg, wspd_vec,      wdir, wstd, wgust, precip, solarq, solarq_un, kdown, kdown_dif, Sun, Ldown = [c.text for c in cols]
        print Date_time, Record, Tair, Tdew, RH, pstn, pmsl, wspd_avg, wspd_vec, wdir, wstd, wgust, precip, solarq, solarq_un, kdown, kdown_dif, Sun, Ldown

我似乎得到了一个错误: 回溯(最近一次呼叫): 文件“..\data.py”,第14行,in 行=table.findAll('tr') AttributeError:“NoneType”对象没有属性“findAll”

请原谅我对靓汤的无知。我对其他方法完全开放。 我的目标是将表中的最后一行放入变量中,这样我就可以进行趋势分析


Tags: in信息http网页datahtmltable趋势