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

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

網(wǎng)絡(luò)抓取后無法從字典中檢索值

網(wǎng)絡(luò)抓取后無法從字典中檢索值

呼喚遠(yuǎn)方 2023-01-04 15:35:53
我希望這里的人能夠回答我認(rèn)為是一個(gè)簡單的問題。我是一個(gè)完全的新手,一直在嘗試從網(wǎng)站 Archdaily 創(chuàng)建一個(gè)圖像網(wǎng)絡(luò)爬蟲。經(jīng)過多次調(diào)試后,下面是我的代碼:#### - Webscraping 0.1 alpha -#### - Archdaily - import requestsfrom bs4 import BeautifulSoup# Enter the URL of the webpage you want to download the images frompage = 'https://www.archdaily.com/63267/ad-classics-house-vi-peter-eisenman/5037e0ec28ba0d599b000190-ad-classics-house-vi-peter-eisenman-image'# Returns the webpage source code under page_docresult = requests.get(page)page_doc = result.content# Returns the source code as BeautifulSoup object, as nested data structuresoup = BeautifulSoup(page_doc, 'html.parser')img = soup.find('div', class_='afd-gal-items')img_list = img.attrs['data-images']for k, v in img_list():    if k == 'url_large':        print(v)這些元素在這里:img = soup.find('div', class_='afd-gal-items')img_list = img.attrs['data-images']嘗試隔離 data-images 屬性,如下所示:這部分我github上傳,很長如您所見,或者我在這里完全錯(cuò)了,我嘗試從這個(gè)最終字典列表中調(diào)用“url_large”值時(shí)出現(xiàn)了 TypeError,如下所示:Traceback (most recent call last):  File "D:/Python/Programs/Webscraper/Webscraping v0.2alpha.py", line 23, in <module>    for k, v in img_list():TypeError: 'str' object is not callable我相信我的錯(cuò)誤在于由此產(chǎn)生的“數(shù)據(jù)圖像”隔離,對(duì)我來說它看起來像列表中的字典,因?yàn)樗鼈儽环嚼ㄌ?hào)和大括號(hào)括起來。我在這里完全不適應(yīng),因?yàn)槲一旧鲜敲つ康剡M(jìn)入這個(gè)項(xiàng)目的(甚至還沒有讀過 Guttag 的書的第 4 章)。我也到處尋找想法,并試圖模仿我發(fā)現(xiàn)的東西。我發(fā)現(xiàn)其他人之前提供的將數(shù)據(jù)更改為 JSON 數(shù)據(jù)的解決方案,所以我找到了以下代碼:jsonData = json.loads(img.attrs['data-images'])print(jsonData['url_large'])但這是一個(gè)半身像,如下所示:Traceback (most recent call last):  File "D:/Python/Programs/Webscraper/Webscraping v0.2alpha.py", line 29, in <module>    print(jsonData['url_large'])TypeError: list indices must be integers or slices, not str在更改這些字符串值時(shí)我缺少一個(gè)步驟,但我不確定在哪里可以更改它們。希望有人能幫我解決這個(gè)問題,謝謝!
查看完整描述

3 回答

?
GCT1015

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

這都是關(guān)于類型的。


img_list實(shí)際上不是一個(gè)列表,而是一個(gè)字符串。您嘗試調(diào)用它img_list()會(huì)導(dǎo)致錯(cuò)誤。


您有正確的想法,可以使用json.loads. 這里的錯(cuò)誤非常簡單——jsonData是一個(gè)列表,而不是字典。你有不止一張圖片。


您可以遍歷列表。列表中的每個(gè)項(xiàng)目都是一個(gè)字典,您將能夠url_large在列表中的每個(gè)字典中找到該屬性:


images_json = img.attrs['data-images']

for image_properties in json.loads(images_json):

    print(image_properties['url_large'])


查看完整回答
反對(duì) 回復(fù) 2023-01-04
?
ITMISS

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

我也想更明確地說明我在您的代碼中看到的內(nèi)容。

在這個(gè)特定的塊中:

img_list = img.attrs['data-images'] for k, v in img_list():    if k == 'url_large':        print(v)

有幾個(gè)語法錯(cuò)誤。如果“img_list”真的是一本字典,你就不能用這種方式遍歷它。您需要在第二行使用 img_list.items() (對(duì)于 python3)或 img_list.iteritems() (python2)。

當(dāng)你像那樣使用括號(hào)時(shí),意味著你正在調(diào)用一個(gè)函數(shù)。但在這里,您正試圖遍歷字典。這就是為什么您會(huì)收到“不可調(diào)用”錯(cuò)誤的原因。

另一個(gè)主要問題是類型問題。simic0de 和 Infinity 解決了這個(gè)問題,但最終您需要檢查 img_list 的類型并根據(jù)需要進(jìn)行轉(zhuǎn)換,以便您可以遍歷它。


查看完整回答
反對(duì) 回復(fù) 2023-01-04
?
慕虎7371278

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

錯(cuò)誤來源: img_list是一個(gè)字符串。您必須將其轉(zhuǎn)換為列表 usingjson.loads并且它不會(huì)成為您必須循環(huán)的字典列表。


工作解決方案:


import json

import requests

from bs4 import BeautifulSoup


# Enter the URL of the webpage you want to download the images from

page = 'https://www.archdaily.com/63267/ad-classics-house-vi-peter-eisenman/5037e0ec28ba0d599b000190-ad-classics-house-vi-peter-eisenman-image'


# Returns the webpage source code under page_doc

result = requests.get(page)

page_doc = result.content


# Returns the source code as BeautifulSoup object, as nested data structure

soup = BeautifulSoup(page_doc, 'html.parser')

img = soup.find('div', class_='afd-gal-items')

img_list = img.attrs['data-images']

for img in json.loads(img_list):

    for k, v in img.items():

        if k == 'url_large':

            print(v)


查看完整回答
反對(duì) 回復(fù) 2023-01-04
  • 3 回答
  • 0 關(guān)注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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