遍历嵌套的 JSON 对象并在整个过程中获取值
Iterate through nested JSON object and get values throughout
从事 API 项目,我试图从 API 输出中获取所有重定向 URL,例如 https://urlscan.io/api/v1/result/39a4fc22-39df-4fd5-ba13-21a91ca9a07d/
我尝试从中提取网址的示例:
"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 的最佳方法是什么。
A 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}')
从事 API 项目,我试图从 API 输出中获取所有重定向 URL,例如 https://urlscan.io/api/v1/result/39a4fc22-39df-4fd5-ba13-21a91ca9a07d/
我尝试从中提取网址的示例:
"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 的最佳方法是什么。
A 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}')