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

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

網絡抓取后無法從字典中檢索值

網絡抓取后無法從字典中檢索值

呼喚遠方 2023-01-04 15:35:53
我希望這里的人能夠回答我認為是一個簡單的問題。我是一個完全的新手,一直在嘗試從網站 Archdaily 創建一個圖像網絡爬蟲。經過多次調試后,下面是我的代碼:#### - 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上傳,很長如您所見,或者我在這里完全錯了,我嘗試從這個最終字典列表中調用“url_large”值時出現了 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我相信我的錯誤在于由此產生的“數據圖像”隔離,對我來說它看起來像列表中的字典,因為它們被方括號和大括號括起來。我在這里完全不適應,因為我基本上是盲目地進入這個項目的(甚至還沒有讀過 Guttag 的書的第 4 章)。我也到處尋找想法,并試圖模仿我發現的東西。我發現其他人之前提供的將數據更改為 JSON 數據的解決方案,所以我找到了以下代碼:jsonData = json.loads(img.attrs['data-images'])print(jsonData['url_large'])但這是一個半身像,如下所示: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在更改這些字符串值時我缺少一個步驟,但我不確定在哪里可以更改它們。希望有人能幫我解決這個問題,謝謝!
查看完整描述

3 回答

?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

這都是關于類型的。


img_list實際上不是一個列表,而是一個字符串。您嘗試調用它img_list()會導致錯誤。


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


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


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

for image_properties in json.loads(images_json):

    print(image_properties['url_large'])


查看完整回答
反對 回復 2023-01-04
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

我也想更明確地說明我在您的代碼中看到的內容。

在這個特定的塊中:

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

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

當你像那樣使用括號時,意味著你正在調用一個函數。但在這里,您正試圖遍歷字典。這就是為什么您會收到“不可調用”錯誤的原因。

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


查看完整回答
反對 回復 2023-01-04
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

錯誤來源: img_list是一個字符串。您必須將其轉換為列表 usingjson.loads并且它不會成為您必須循環的字典列表。


工作解決方案:


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)


查看完整回答
反對 回復 2023-01-04
  • 3 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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