网页抓取时出现AttributeError

网络抓取时收到 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'


手掌心
浏览 133回答 1
1回答

回首忆惘然

您从jurisdiction列表开始,然后立即将其作为字典。然后,您将其视为 dict,直到您尝试再次将其视为列表的错误行。我认为您在开始时需要为列表命名。可能您的意思是司法管辖区(复数)作为列表。但是,IMO 还有另外两个领域也肯定需要修复:find返回一个表。dict 中的标签/键表示您想要稍后的表(不是第一个匹配项)您的目标表的索引不正确你想要这样的东西:import requests, refrom bs4 import BeautifulSoupresponse_obj = requests.get('https://en.wikipedia.org/wiki/Demographics_of_New_York_City').textsoup = 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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python