Python:属性错误和findA

2024-03-29 01:59:44 发布

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

我使用的是Python2.7,我正在尝试对一个包含表的网站进行web清理。我一直收到这个错误信息: AttributeError:addinfourl实例没有属性'findAll'

我是不是用错了“findAll”?谢谢!你知道吗

wind = urllib2.urlopen('http://w1.weather.gov/data/obhistory/KCQX.html')
# print(third_page)
tables = wind.findAll('table')
data_table = tables[3]
rows = data_table.findAll('tr')
output_matrix = []
for row in rows:
    subrow = row.findAll('td')
    new_row = []
    if(len(subrow)>0):
        temp_row = []
        for subsubrow in subrow:
            temp_row.append(subsubrow.get_text().strip())
        output_matrix.append(temp_row)

Tags: inforoutputtablesdatatabletempmatrix
1条回答
网友
1楼 · 发布于 2024-03-29 01:59:44

wind变量是一个类似文件的对象,它不包含findAll方法。如果您想美化soup,您需要从页面内容创建一个新的“soup”:

from bs4 import BeautifulSoup
import urllib2

html = urllib2.urlopen('http://w1.weather.gov/data/obhistory/KCQX.html').read()
wind = BeautifulSoup(html)

BeautifulSoup的构造函数也可以采用类似文件的对象,因此可以删除最后一个.read()

html = urllib2.urlopen('http://w1.weather.gov/data/obhistory/KCQX.html')
wind = BeautifulSoup(html)

相关问题 更多 >