亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

網頁抓取時出現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 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號