Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Save json request to a file

thanks for your attention!

I’m doing a for loop and "requesting url" here on python and getting a "json" response, want to add all the loops to a file to read later on excel, or other json reader…

for x in range(1, 6):
    url = f"https://api.xxx.com.br/ecommerce-web/api/parceiro/meuCadastro/{x}/true"
    response = requests.get(url, headers=headers)
    print(response.json())

So after this loop I got 5 responses like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'A': 1, 'Name': 'Name01'}
{'A': 2, 'Name': 'Name02'}
{'A': 3, 'Name': 'Name03'}
{'A': 4, 'Name': 'Name04'}
{'A': 5, 'Name': 'Name05'}

And i want to do a json file with all that answers, thank you guys! I’m just starting on webscraping.

Solve this problem add all the answers on json file

>Solution :

You could use the built-in json library to write json files.

import json

data = []  # save all responses here

for x in range(1, 6):
    url = f"https://api.xxx.com.br/ecommerce-web/api/parceiro/meuCadastro/{x}/true"
    response = requests.get(url, headers=headers)
    data.append(response.json())  # append every json response


with open("myjson.json", "w") as file:  # "w" will create myjson.json if it doesn't exist, otherwise rewrite the whole file
    json.dump(data, file, indent=4)  # dump all the data into the json file with 4 space indentation

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading