I am running a query against an API of the following
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
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
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
URL1 App_Name Category
I am trying getting the category and the app_name dumped into a file but I am struggling
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?
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.
thank you
CodePudding user response:
change for i in list['data']:
to for i in resp.json()['data']:
.
CodePudding user response:
what is list
in this code block:
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:
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"]:
...
P.S. best to avoid naming variables with names used for built-in python functions or types (e.g. list
)