我正在尝试抓取此页面的 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])
答案 0 :(得分:0)
运行返回的 json
对象上方的代码是一个列表。所以 data[id]
试图用 str
索引到一个列表中,但它不起作用
这是我对 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'}]}
答案 1 :(得分:0)
您无法使用密钥访问列表。
以下是导致问题的行,以及我认为您想要的代码:
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
每当你操作数据时,打印出来看看如何得到你想要的。