import requests
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"}
url = f'https://api.sofascore.com/api/v1/sport/football/events/live'
response = requests.get(url, headers=headers).json()
events = response['events']
for event in events:
List_of_Urls = []
List_of_Urls.append(event['id'])
slug = event['slug']
for List_of_Url in List_of_Urls:
try:
url2 = f'https://api.sofascore.com/api/v1/event/{List_of_Url}/graph'
response2 = requests.get(url2, headers=headers).json()
if response2['graphPoints']:
print(slug)
except:
pass
回應 JSON 示例 1:https : //api.sofascore.com/api/v1/event/9625897/graph
{
"graphPoints": [
{
"minute": 1,
"value": -2
}
回應 JSON 示例 2:https : //api.sofascore.com/api/v1/event/9761921/graph
{
"error": {
"code": 404,
"message": "Not Found"
}
}
我的想法是,如果有graphPointsin JSON,則列印該slug值,但在if response2['graphPoints']:for的直接值true or false中不起作用,我應該如何使其作業?
uj5u.com熱心網友回復:
如果它是一個 JSON 物件,我認為你可以這樣做:
if 'graphPoints' in response2:
print(slug)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/376095.html
