第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

網(wǎng)頁(yè)抓取時(shí)出現(xiàn)AttributeError

網(wǎng)頁(yè)抓取時(shí)出現(xiàn)AttributeError

手掌心 2022-05-11 14:30:34
網(wǎng)絡(luò)抓取時(shí)收到 AttributeError 但我不確定我做錯(cuò)了什么?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'})準(zhǔn)備表    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) 屬性錯(cuò)誤   ---> 18     jurisdiction.append(jurisdiction)   AttributeError: 'dict' object has no attribute 'append'
查看完整描述

1 回答

?
回首憶惘然

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊

您從jurisdiction列表開始,然后立即將其作為字典。然后,您將其視為 dict,直到您嘗試再次將其視為列表的錯(cuò)誤行。我認(rèn)為您在開始時(shí)需要為列表命名。可能您的意思是司法管轄區(qū)(復(fù)數(shù))作為列表。但是,IMO 還有另外兩個(gè)領(lǐng)域也肯定需要修復(fù):

  1. find返回一個(gè)表。dict 中的標(biāo)簽/鍵表示您想要稍后的表(不是第一個(gè)匹配項(xiàng))

  2. 您的目標(biāo)表的索引不正確

你想要這樣的東西:


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)


查看完整回答
反對(duì) 回復(fù) 2022-05-11
  • 1 回答
  • 0 關(guān)注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)