【问题标题】:Iterate through nested JSON object and get values throughout遍历嵌套的 JSON 对象并在整个过程中获取值
【发布时间】:2021-11-28 19:37:19
【问题描述】:

在一个 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 的最佳方法。

【问题讨论】:

    标签: python json parsing


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-04-21
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 2019-07-25
      相关资源
      最近更新 更多