为了账号安全,请及时绑定邮箱和手机立即绑定

网页抓取时出现AttributeError

网页抓取时出现AttributeError

手掌心 2022-05-11 14:30:34
网络抓取时收到 AttributeError 但我不确定我做错了什么?AttributeError 是什么意思?    response_obj = requests.get('https://en.wikipedia.org/wiki/Demographics_of_New_York_City').text    soup = BeautifulSoup(response_obj,'lxml')    Population_Census_Table = soup.find('table', {'class':'wikitable sortable'})准备表    rows = Population_Census_Table.select("tbody > tr")[3:8]    jurisdiction = []    for row in rows:        jurisdiction = {}        tds = row.select('td')        jurisdiction["jurisdiction"] = tds[0].text.strip()        jurisdiction["population_census"] = tds[1].text.strip()        jurisdiction["%_white"] = float(tds[2].text.strip().replace(",",""))        jurisdiction["%_black_or_african_amercian"] = float(tds[3].text.strip().replace(",",""))        jurisdiction["%_Asian"] = float(tds[4].text.strip().replace(",",""))        jurisdiction["%_other"] = float(tds[5].text.strip().replace(",",""))        jurisdiction["%_mixed_race"] = float(tds[6].text.strip().replace(",",""))        jurisdiction["%_hispanic_latino_of_other_race"] = float(tds[7].text.strip().replace(",",""))        jurisdiction["%_catholic"] = float(tds[7].text.strip().replace(",",""))        jurisdiction["%_jewish"] = float(tds[8].text.strip().replace(",",""))            jurisdiction.append(jurisdiction)` `print(jurisdiction) 属性错误   ---> 18     jurisdiction.append(jurisdiction)   AttributeError: 'dict' object has no attribute 'append'
查看完整描述

1 回答

?
回首忆惘然

TA贡献1847条经验 获得超11个赞

您从jurisdiction列表开始,然后立即将其作为字典。然后,您将其视为 dict,直到您尝试再次将其视为列表的错误行。我认为您在开始时需要为列表命名。可能您的意思是司法管辖区(复数)作为列表。但是,IMO 还有另外两个领域也肯定需要修复:

  1. find返回一个表。dict 中的标签/键表示您想要稍后的表(不是第一个匹配项)

  2. 您的目标表的索引不正确

你想要这样的东西:


import requests, re

from bs4 import BeautifulSoup


response_obj = requests.get('https://en.wikipedia.org/wiki/Demographics_of_New_York_City').text

soup = BeautifulSoup(response_obj,'lxml')

Population_Census_Table = soup.select_one('.wikitable:nth-of-type(5)') #use css selector to target correct table.

jurisdictions = []

rows = Population_Census_Table.select("tbody > tr")[3:8]

for row in rows:

    jurisdiction = {}

    tds = row.select('td')

    jurisdiction["jurisdiction"] = tds[0].text.strip()

    jurisdiction["population_census"] = tds[1].text.strip()

    jurisdiction["%_white"] = float(tds[2].text.strip().replace(",",""))

    jurisdiction["%_black_or_african_amercian"] = float(tds[3].text.strip().replace(",",""))

    jurisdiction["%_Asian"] = float(tds[4].text.strip().replace(",",""))

    jurisdiction["%_other"] = float(tds[5].text.strip().replace(",",""))

    jurisdiction["%_mixed_race"] = float(tds[6].text.strip().replace(",",""))

    jurisdiction["%_hispanic_latino_of_other_race"] = float(tds[7].text.strip().replace(",",""))

    jurisdiction["%_catholic"] = float(tds[10].text.strip().replace(",",""))

    jurisdiction["%_jewish"] = float(tds[12].text.strip().replace(",",""))

    jurisdictions.append(jurisdiction)


查看完整回答
反对 回复 2022-05-11
  • 1 回答
  • 0 关注
  • 132 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信