简体   繁体   English

python将某些http响应json值保存到文件中

[英]python saving certain http response json values into file

I am running a query against an API of the following我正在针对以下 API 运行查询

https://test.url/api/v2/services/level/app?domainname={}&offset=0&limit=100 https://test.url/api/v2/services/level/app?domainname={}&offset=0&limit=100

I am getting the domains from a txt file called domains.txt我从一个名为 domain.txt 的 txt 文件中获取域

with open('domains.txt') as f_input:
for id in f_input:
    url = "https://test.url/api/v2/services/level/app?domainname={}&offset=0&limit=100".format(id.strip())
    resp = requests.get(url, headers={'Accept':'application/json','Api-Token': 'XXXX'})
    json_string = json.dumps(resp.json(), sort_keys=True, indent=4, separators=(',', ': '))
    print(json_string)

the domains.txt contains domain.txt 包含

url1.com
url2.com
url3.com

in my response I am getting the following:在我的回复中,我得到以下信息:

    "data": [
    {
        "app_name": "App1",
        "category_name": "Category1",
        "level": 44,
        "indicator": "poor",
        "id": 9563
    }
],
"status": "Success",
"status_code": 200,
"total_query_count": 1

} }

I now want to write the App_name alongside the Category together with the corresponding URLs from domain.txt into a new text file so that eventually I end up with我现在想将 App_name 与 Category 以及 domain.txt 中的相应 URL 一起写入一个新的文本文件,以便最终得到

URL1 App_Name Category URL1 App_Name 类别

I am trying getting the category and the app_name dumped into a file but I am struggling我正在尝试将类别和 app_name 转储到文件中,但我很挣扎

with open("ergebnis.txt", "w") as f:
  for i in list['data']:
    f.write("{0} {1}\n".format(i["app_name"],str(i["category_name"])))

I am getting我正进入(状态

    Traceback (most recent call last):
  File "/Users/xxxx/Desktop/test/test.py", line 25, in <module>
    for i in list['data']:
TypeError: 'types.GenericAlias' object is not iterable

Can anybody help me writing (and perhaps merging) URL App_Name and Category in a file?任何人都可以帮我在文件中编写(并可能合并)URL App_Name 和 Category 吗?

EDIT:编辑:

with open('domains.txt') as f_input:
for id in f_input:
    url = "https://test.url/api/v2/services/level/app?domainname={}&offset=0&limit=100".format(id.strip())
    resp = requests.get(url, headers={'Accept':'application/json','Api-Token': 'XXX'})
    # json_string = json.dumps(resp.json(), sort_keys=True, indent=4, separators=(',', ': '))
    resp_json = resp.json()
    for i in resp_json["data"]:
        with open("ergebnis.txt", "a") as f:
            f.write("{0} {1}\n".format(i["app_name"],str(i["category_name"])))

made it work, I am now getting both the app_name and category_name in each line.让它工作,我现在在每一行都得到 app_name 和 category_name 。

thank you谢谢你

for i in list['data']:更改for i in resp.json()['data']:

what is list in this code block:此代码块中的list是什么:

with open("ergebnis.txt", "w") as f:
  for i in list['data']:
    f.write("{0} {1}\n".format(i["app_name"],str(i["category_name"])))

based on the code you provided most likely you need to iterate through the json response:根据您提供的代码,您很可能需要遍历 json 响应:

with open('domains.txt') as f_input:
for id in f_input:
    url = "https://test.url/api/v2/services/level/app?domainname={}&offset=0&limit=100".format(id.strip())
    resp = requests.get(url, headers={'Accept':'application/json','Api-Token': 'XXXX'})
     
    resp_json = resp.json()
    for i in resp_json["data"]:
        ...

PS best to avoid naming variables with names used for built-in python functions or types (eg list ) PS最好避免使用用于内置python函数或类型的名称命名变量(例如list

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

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