I Have to extract specific multiple values and print those specific values in a file if possible.
I tried the below code to do this
JSON value from URL is:
{'data': [{'value': '0.0.0.0'}, {'value': '0.0.0.1'}, {'value': '0.0.0.2'}]}
import requests
import json
url = 'https://www.example.com'
response = requests.get('url', headers=headers , verify=False)
json_data = json.loads(response.text)
value = json_data['data'][0]['value']
print (value)
output of this : 0.0.0.0
But i want to print in a file(.txt) all these values like below:
0.0.0.0
0.0.0.1
0.0.0.3
Please help me on this.
解决方案
What you want is a loop
json_data = {'data': [{'value': '0.0.0.0'}, {'value': '0.0.0.1'}, {'value': '0.0.0.2'}]}
for x in json_data['data']:
print (x['value'])