简体   繁体   English

遍历嵌套的 JSON object 并在整个过程中获取值

[英]Iterate through nested JSON object and get values throughout

Working on a API project, in which I'm trying to get all the redirect urls from an API output such as https://urlscan.io/api/v1/result/39a4fc22-39df-4fd5-ba13-21a91ca9a07d/在 API 项目上工作,我试图从 API output 获取所有重定向 url,例如https://urlscan.io/api/v1/result/39a4fc22-39df-4fd5-ab913-21

Example of where I'm trying to pull the urls from:我试图从中提取网址的示例:

"redirectResponse": {
  "url": "https://www.coke.com/"

I currently have the following code:我目前有以下代码:

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}")

I get the following error: TypeError: list indices must be integers or slices, not str .我收到以下错误: TypeError: list indices must be integers or slices, not str Not sure what the best way to parse the nested json in order to get all the redirect urls.不确定解析嵌套 json 以获得所有重定向 URL 的最佳方法是什么。

A redirectResponse isn't always present in the requests , so the code has to be written to handle that and keep going. redirectResponse并不总是出现在requests中,因此必须编写代码来处理它并继续进行。 In Python that's usually done with a try / except :在 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}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM