[英]“TypeError: list indices must be integers or slices, not str”
[英]i get TypeError: list indices must be integers or slices, not str
我正在嘗試抓取此頁面的 json 腳本,但由於某種原因我收到此錯誤
import requests
import json
from bs4 import BeautifulSoup
url = "https://www.amongstfew.com/drops"
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
script = soup.find("script", class_="draw_data_json").string.strip()
data = json.loads(script)
print(data['id']['title']['published_scope']['variants'][0])
運行您在json
object 上方返回的代碼是一個列表。 所以data[id]
試圖用一個不起作用的str
索引到一個列表中
這是我的 output 用於data[0]
In [9]: data[0]
Out[9]:
{'id': 1579513610342,
'title': 'adidas - Yeezy BOOST 350 V2 (Sesame/Sesame/Sesame)',
'published_scope': 'global',
'variants': [{'id': 15564393054310,
'product_id': 1579513610342,
'title': 'US 5 / UK 4.5 / EU 37 1/3',
'price': '999.00',
'sku': 'F99710-US 5 / UK 4.5 / EU 37 1/3',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564393119846,
'product_id': 1579513610342,
'title': 'US 5.5 / UK 5 / EU 38',
'price': '999.00',
'sku': 'F99710-US 5.5 / UK 5 / EU 38',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564393152614,
'product_id': 1579513610342,
'title': 'US 6 / UK 5.5 / EU 38 2/3',
'price': '999.00',
'sku': 'F99710- US 6 / UK 5.5 / EU 38 2/3',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564393185382,
'product_id': 1579513610342,
'title': 'US 6.5 / UK 6 / EU 39 1/3',
'price': '999.00',
'sku': 'F99710-US 6.5 / UK 6 / EU 39 1/3',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564393218150,
'product_id': 1579513610342,
'title': 'US 7 / UK 6.5 / EU 40',
'price': '999.00',
'sku': 'F99710- US 7 / UK 6.5 / EU 40',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564393250918,
'product_id': 1579513610342,
'title': 'US 7.5 / UK 7 / EU 40 2/3',
'price': '999.00',
'sku': 'F99710- US 7.5 / UK 7 / EU 40 2/3',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564393283686,
'product_id': 1579513610342,
'title': 'US 8 / UK 7.5 / EU 41 1/3',
'price': '999.00',
'sku': 'F99710-US 8 / UK 7.5 / EU 41 1/3',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564393316454,
'product_id': 1579513610342,
'title': 'US 8.5 / UK 8 / EU 42',
'price': '999.00',
'sku': 'F99710- US 8.5 / UK 8 / EU 42',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564393349222,
'product_id': 1579513610342,
'title': 'US 9 / UK 8.5 / EU 42 2/3',
'price': '999.00',
'sku': 'F99710- US 9 / UK 8.5 / EU 42 2/3',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564393381990,
'product_id': 1579513610342,
'title': 'US 9.5 / UK 9 / EU 43 1/3',
'price': '999.00',
'sku': 'F99710-US 9.5 / UK 9 / EU 43 1/3',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564393414758,
'product_id': 1579513610342,
'title': 'US 10 / UK 9.5 / EU 44',
'price': '999.00',
'sku': 'F99710-US 10 / UK 9.5 / EU 44',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564400001126,
'product_id': 1579513610342,
'title': 'US 10.5 / UK 10 / EU 44 2/3',
'price': '999.00',
'sku': 'F99710-US 10.5 / UK 10 / EU 44 2/3',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564400033894,
'product_id': 1579513610342,
'title': 'US 11 / UK 10.5 / EU 45 1/3',
'price': '999.00',
'sku': 'F99710- US 11 / UK 10.5 / EU 45 1/3',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564400066662,
'product_id': 1579513610342,
'title': 'US 11.5 / UK 11 / EU 46',
'price': '999.00',
'sku': 'F99710-US 11.5 / UK 11 / EU 46',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564400099430,
'product_id': 1579513610342,
'title': 'US 12 / UK 11.5 / EU 46 2/3',
'price': '999.00',
'sku': 'F99710-US 12 / UK 11.5 / EU 46 2/3',
'compare_at_price': '0.00',
'inventory_quantity': 0},
{'id': 15564400132198,
'product_id': 1579513610342,
'title': 'US 12.5 / UK 12 / EU 47 1/3',
'price': '999.00',
'sku': 'F99710-US 12.5 / UK 12 / EU 47 1/3',
'compare_at_price': '0.00',
'inventory_quantity': 0}],
'images': [{'id': 4849383309414,
'product_id': 1579513610342,
'alt': None,
'src': 'https://cdn.shopify.com/s/files/1/1028/1839/products/yeezy-boost-350-v2---f99710---pair.jpg?v=1542971701'},
{'id': 4849383571558,
'product_id': 1579513610342,
'alt': None,
'src': 'https://cdn.shopify.com/s/files/1/1028/1839/products/yeezy-boost-350-v2---f99710---side.jpg?v=1542971701'},
{'id': 4849382522982,
'product_id': 1579513610342,
'alt': None,
'src': 'https://cdn.shopify.com/s/files/1/1028/1839/products/yeezy-boost-350-v2---f99710---front.jpg?v=1542971701'},
{'id': 4849382424678,
'product_id': 1579513610342,
'alt': None,
'src': 'https://cdn.shopify.com/s/files/1/1028/1839/products/yeezy-boost-350-v2---f99710---back.jpg?v=1542971701'}]}
您無法訪問帶有鍵的列表。
下面是導致麻煩的行,以及我認為你想要的代碼:
import requests
import json
from bs4 import BeautifulSoup
url = "https://www.amongstfew.com/drops"
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
script = soup.find("script", class_="draw_data_json").string.strip()
data = json.loads(script)
for item in data:
print(data["id"] data["title"], data["published_scope"], data["variants"][0]])
# print(data['id']['title']['published_scope']['variants'][0]) # this line is causing trouble
每當您操作數據時,將其打印出來,看看如何得到您想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.