正在处理一个API项目,在该项目中,我试图从API输出中获取所有重定向URL,例如https://urlscan.io/api/v1/result/39a4fc22-39df-4fd5-ba13-21a91ca9a07d/
我试图从哪里提取URL的示例:
"redirectResponse": {
"url": "https://www.coke.com/"
我目前有以下代码:
import requests
import json
import time
#URL to be scanned
url = 'https://www.coke.com'
#URL Scan Headers
headers = {'API-Key':apikey,'Content-Type':'application/json'}
data = {"url":url, "visibility": "public"}
response = requests.post('https://urlscan.io/api/v1/scan/',headers=headers, data=json.dumps(data))
uuid = response.json()['uuid']
responseUrl = response.json()['api']
time.sleep(10)
req = requests.Session()
r = req.get(responseUrl).json()
r.keys()
for value in r['data']['requests']['redirectResponse']['url']:
print(f"{value}")
我得到以下错误:TypeError: list indices must be integers or slices, not str
。不确定解析嵌套json以获得所有重定向url的最佳方式是什么。
redirectResponse
并不总是存在于requests
中,因此必须编写代码来处理它并继续运行。在Python中,通常使用try
/except
:
for obj in r['data']['requests']:
try:
redirectResponse = obj['request']['redirectResponse']
except KeyError:
continue # Ignore and skip to next one.
url = redirectResponse['url']
print(f'{url=!r}')