我有下面的代碼可以完全正常作業,直到我設定x=37. 此時,我收到錯誤
TypeError:'NoneType' 物件在變數 t["vintage"]["wine"]["region"]["country"]["name"] 上不可下標。
我添加了另一個變數,幾乎每次都會發生相同的問題,因此您可能會在那里找到錯誤。
我認為這是因為該頁面上的 25 個結果之一沒有分配國家名稱,因此變數給出了錯誤。
我想我需要為每個變數添加一個例外來處理這種情況。我已經看到了添加這些的示例,除了它們似乎處于請求級別,沒有找到合法頁面而不是變數之一,我找不到在變數級別添加它們的指導。
# Import packages
import requests
import json
import pandas as pd
import time
x=37
# Get request from the Vivino website
r = requests.get(
"https://www.vivino.com/api/explore/explore",
params={
#"country_code": "FR",
#"country_codes[]":"pt",
"currency_code":"GBP",
"grape_filter":"varietal",
"min_rating":"1",
"order_by":"price",
"order":"asc",
"page": x,
"price_range_max":"100",
"price_range_min":"25",
"wine_type_ids[]":"1"
},
headers= {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"
},
)
# Variables to scrape from the Vivino website
results = [
(
t["vintage"]["wine"]["winery"]["name"],
t["vintage"]["year"],
t["vintage"]["wine"]["id"],
t["vintage"]["wine"]["name"],
t["vintage"]["statistics"]["ratings_average"],
t["prices"][0]["amount"],
t["vintage"]["wine"]["region"]["country"]["name"],
t["vintage"]["wine"]["region"]["country"]["code"],
t["vintage"]["wine"]["region"]["name"],
t["vintage"]["wine"]["style"]["name"]
)
for t in r.json()["explore_vintage"]["matches"]
]
# Saving the results in a dataframe
dataframe = pd.DataFrame(
results,
columns=["Winery", "Vintage", "Wine ID", "Wine", "Rating", "Price", "Country", "CountryCode", "Region", "Style"]
)
#output the dataframe
df_out = dataframe
df_out.to_csv("data.csv", index=False)
print("Complete -",x,"iterations")
uj5u.com熱心網友回復:
問題是一些鍵在深度嵌套的字典中隨機丟失(用 None 表示)。展示斗爭的樣本字典:
data = [
{'k1': {'k2': {'k3': 'value_i_want'}}},
{'k1': {'k2': None}},
{'k1': {'k2': {'k3': 'value_i_want'}}},
]
當您假設鍵k3肯定存在于陣列中的每個字典中時,它不存在。因此,當您嘗試做類似的事情時
result = [t['k1']['k2']['k3'] for t in data]
你得到TypeError: 'NoneType' object is not subscriptable.
TypeErrort['k1']['k2']當在 for 回圈下的第二次迭代中求值時出現None,并且您嘗試在其中查找鍵。您基本上是在要求程式執行None['k3'],這解釋了您收到的錯誤訊息。
要解決這個問題(這在 API 請求回傳的資料中很常見),您需要嘗試捕獲該塊。您可能會發現此輔助函式很有用:
def try_to_get(d: dict, *args, default=None):
try:
for k in args:
d = d[k]
return d
except (KeyError, TypeError) as _:
print(f'Cannot find the key {args}')
return default
使用輔助函式,我們可以撰寫try_to_get(t, 'k1, 'k2', 'k3). 雖然沒有問題的字典會遍歷嵌套并獲取您想要的值,但有問題的字典會觸發 Exception 塊并在出現錯誤時回傳一個默認值(這里,默認值為 None)。
您可以嘗試將代碼中的串列理解部分替換為:
results = [
(
try_to_get(t, "vintage", "wine", "winery", "name"),
try_to_get(t, "vintage", "year"),
try_to_get(t, "vintage", "wine", "id"),
try_to_get(t, "vintage", "wine", "name"),
try_to_get(t, "vintage", "statistics", "ratings_average"),
try_to_get(t, "prices", 0, "amount"),
try_to_get(t, "vintage", "wine", "region", "country", "name"),
try_to_get(t, "vintage", "wine", "region", "country", "code"),
try_to_get(t, "vintage", "wine", "region", "name"),
try_to_get(t, "vintage", "wine", "style", "name"),
)
for t in r.json()["explore_vintage"]["matches"]
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/465105.html
