如果你想处理 json 请求的响应,可以试试这个:
import requests
url = "https://webapi.depop.com/api/v2/search/products/?brands=1645&itemsPerPage=24&country=gb¤cy=GBP&sort=relevance"
payload={}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
所以你的输出是这样的:
{
"meta": {
"resultCount": 20,
"cursor": "MnwyMHwxNjQwMDA1ODc3",
"hasMore": false,
"totalCount": 20
},
"products": [
{
"id": 215371070,
"slug": "kicksbrothers-exclusive-genuine-blue-inc",
"status": "ONSALE",
"hasVideo": false,
"price": {
"priceAmount": "22.98",
"currencyName": "GBP",
"nationalShippingCost": "4.99",
"internationalShippingCost": "10.00"
},
"preview": {
"150": "https://pictures.depop.com/b0/24241961/1015682639_ea92c00979b64a298f7b9cce465bfb5f/P2.jpg",
"210": "https://pictures.depop.com/b0/24241961/1015682639_ea92c00979b64a298f7b9cce465bfb5f/P4.jpg",
"320": "https://pictures.depop.com/b0/24241961/1015682639_ea92c00979b64a298f7b9cce465bfb5f/P5.jpg",
"480": "https://pictures.depop.com/b0/24241961/1015682639_ea92c00979b64a298f7b9cce465bfb5f/P6.jpg",
"640": "https://pictures.depop.com/b0/24241961/1015682639_ea92c00979b64a298f7b9cce465bfb5f/P1.jpg",
"960": "https://pictures.depop.com/b0/24241961/1015682639_ea92c00979b64a298f7b9cce465bfb5f/P7.jpg",
"1280": "https://pictures.depop.com/b0/24241961/1015682639_ea92c00979b64a298f7b9cce465bfb5f/P8.jpg"
},
"variantSetId": 93,
"variants": {
"7": 1
},
"isLiked": false
},
如何解析json响应
import requests
import json
def get_requests():
url = "https://webapi.depop.com/api/v2/search/products/?brands=1645&itemsPerPage=24&country=gb¤cy=GBP&sort=relevance"
payload={}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
return response.text
# x uses method "get_requests"
x = get_requests()
data_json = json.loads(x)
for id, price in zip(data_json['products'], data_json['products']):
print(id['id'])
print(price['price']['priceAmount'])
输出:
215371070
22.98
256715789
8.00
202721541
5.00
202722546
5.00
274328291
24.00
221641139
10.00
245419941
30.00
192541316
8.00
147762409
14.00
158406248
9.99
234693030
20.00
213377081
10.00
228630951
10.00
203627182
16.00
159958157
7.99
151413456
27.20
250985338
8.00
185488012
15.00
154423470
20.00
193888222
10.00
您遍历了 json 响应并保存了键的值:“id”和“price”