Python 我得到TypeError:列表索引必须是整数或片,而不是str
我正在尝试删除此页面的json脚本,但由于某些原因,我出现了此错误Python 我得到TypeError:列表索引必须是整数或片,而不是str,python,beautifulsoup,Python,Beautifulsoup,我正在尝试删除此页面的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&qu
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
对象上方的代码是一个列表。因此,data[id]
正试图用一个str
索引到一个列表中,但这不起作用
这是我对数据[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
每当你操作数据时,把它打印出来,看看如何得到你想要的