Запросы при помощи urllib.request и json. Практика запросов при помощи открытых API
Как упакованы данные на языке стандартных типов Python:
- Внешняя упаковка – словарь, фигурные скобки. В нем три ключа: строки «people» (люди в космосе), «message» (сообщение об успешности передачи данных), «number» (количество людей в космосе).
- Значение по ключу «people» – список, квадратные скобки. В списке еще три словаря: данные о каждом космонавте.
- Внутренняя упаковка: словарь, фигурные скобки. В нем два ключа: «craft» – космический корабль и «name» – имя космонавта.
- сообщение успешно (success) передано,
- в космосе 3 космонавта,
- люди (people) в космосе – это: Andrew Morgan, Олег Скрипочка и Jessica Meir, все на МКС (ISS).
Программа для получения информации и ее печати на экране:
Использовано:
- функция urllib.request.urlopen()
- функция json.load()
- Метод словаря get(). Можно использовать прямое обращение к словарю по ключу: astr[‘name’].
Задание: время пролетов МКС (почти) над Москвой
Задача: получить данные о времени пролетов МКС на небе Москвы. Напечатать на экране:
- Сколько пролетов МКС содержится в ответе (response) на запрос (request)
- Для каждого пролета МКС: время восхода (risetime), длительность (duration), время захода в человекочитаемом виде.
Для перевода unix time в понятные человеку день, месяц, год и часы, минуты, секунды используют библиотеку time, функцию ctime:
Начало программы, которую требуется дописать:
Решение
МКС на вечернем небе – самое яркое тело на небе после Солнца и Луны:
/>
Точное время
Задача: получить точное время и сравнить его с временем компьютера, на котором запускается программа.
Напечатать на экране:
- точную дату и время,
- разницу в минутах и секундах с временем компьютера.
- «На пятерку»: напечатать время рассвета и заката.
Точное время доступно по ключу «time» и представляет собой unix time, но в миллисекундах.
То есть, для получения результата в функцию time.ctime() надо передать значение, поделенное на 1000.
Для получения времени компьютера надо выполнить time.time() без параметров
Для получения времени рассвета и заката надо извлекать вложенные данные подобно матрешке: сначала обратиться к данным по ключу «clocks», потом по ключу «213» (код Москвы), потом у результата этой операции обратиться по ключам «sunrise» и «sunset».
А еще это напоминает сказку про Кощея бессмертного:
Начало программы, которую требуется дописать:
Решение
![]()
Бонусная задача: курс доллара
Задача: получить усредненный курсы рубля к доллару, евро и другим валютам.
Напечатать на экране:
- курс рубля («RUB») к доллару («USD»)
- курс рубля к евро («EUR»)
- курс рубля к самой дешевой и самой дорогой валюте из представленных
Для того, чтобы узнать, сколько рублей стоит валюта, надо вычислить отношение курса рубля к курсу выбранной валюты.
Начало программы, которую требуется дописать:
Решение
Для самостоятельного изучения: справочная информация, примеры открытых API
Решение проблем
Если подключение по сети отсутствует, использовать данные из раздела «Результат запроса». Программа для задачи «Космонавты»:
How to Get JSON from a URL in Python

Being able to retrieve data from remote servers is a fundamental requirement for most projects in web development. JSON is probably one of the most popular formats for data exchange due to its lightweight and easy to understand structure that is fairly easy to parse. Python, being a versatile language, offers a variety of ways to fetch JSON data from a URL in your web project.
In this article, we'll explore how to use Python to retrieve JSON data from a URL. We'll cover two popular libraries — requests and urllib , and show how to extract and parse the JSON data using Python's built-in json module. Additionally, we'll discuss common errors that may occur when fetching JSON data, and how to handle them in your code.
Using the requests Library
One popular library for fetching data from URLs in Python is requests . It provides an easy-to-use interface for sending HTTP requests to retrieve data from remote servers. To use requests , you'll first need to install it by using pip in your terminal:
Once we have requests installed, we can use it to fetch JSON data from a URL using the get() method. Say we want to fetch posts from the dummy API called jsonplaceholder.typicode.com/posts :
We used the get() method to fetch JSON data from the URL https://jsonplaceholder.typicode.com/posts , we extracted the JSON data using the json() method, and printed it to the console. And that's pretty much it! You will get the JSON response stored as a Python list, with each post represented by one dictionary in that list. For example, one post will be represented as the following dictionary:
But, what if the API request returns an error? Well, we'll handle that error by checking the status code we got from the API when sending a GET request:
In addition to what we have already done, we checked the status code of the response to ensure that the request was successful. If the status code is 200 , we print the extracted JSON in the same fashion as before, and if the status code is not 200 we are prompting an error message.
Note: The requests library automatically handles decoding JSON responses, so you don't need to use the json module to parse the response. Instead, you can use the json() method of the response object to extract the JSON data as a Python dictionary or list:
This method will raise a ValueError if the response body does not contain valid JSON.
Using the urllib Library
Python's built-in urllib library provides a simple way to fetch data from URLs. To fetch JSON data from a URL, you can use the urllib.request.urlopen() method:
After fetching the JSON from the API URL of choice, we checked the status code of the response to ensure that the request was successful. If the status code is 200 , we extract the JSON data using the json.loads() method and print the title of each post.
It's worth noting that urllib does not automatically decode response bodies, so we need to use the decode() method to decode the response into a string. We then use the json.loads() method to parse the JSON data:
Advice: If you want to know more about parsing JSON objects in Python, you should definitely read our guide Reading and Writing JSON to a File in Python".
Using the aiohttp Library
In addition to urllib and requests , there is another library that is commonly used for making HTTP requests in Python — aiohttp . It's an asynchronous HTTP client/server library for Python that allows for more efficient and faster requests by using asyncio .
To use aiohttp , you'll need to install it using pip :
Once installed, you can start using it. Let's fetch JSON data from a URL using the aiohttp library:
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
We defined an async function fetch_json that takes a URL as input and uses aiohttp to make an HTTP GET request to that URL. We then used the response.json() method to convert the response data to a Python object.
We also defined an async function main that simply calls fetch_json with a URL and prints the resulting JSON data.
Finally, we used the asyncio.run() function to run the main function and fetch the JSON data asynchronously.
Overall, aiohttp can be a great choice for applications that need to make a large number of HTTP requests or require faster response times. However, it may have a steeper learning curve compared to urllib and requests due to its asynchronous nature and the use of asyncio .
Which Library to Choose?
When choosing a library for getting JSON data from a URL in Python, the decision often comes down to the specific needs of your project. Here are some general guidelines to consider:
- For simple requests or legacy code: If you're making simple requests or working with legacy code, urllib may be a good choice due to its built-in nature and compatibility with older Python versions.
- For ease of use: If ease of use and simplicity are a priority, requests is often the preferred choice. It has a user-friendly syntax and offers many useful features that make it easy to fetch JSON data from a URL.
- For high-performance and scalability: If your application needs to make a large number of HTTP requests or requires faster response times, aiohttp may be the best choice. It offers asynchronous request handling and is optimized for performance.
- For compatibility with other asyncio -based code: If you're already using asyncio in your project or if you need compatibility with other asyncio -based code, aiohttp may be the best choice due to its built-in support for asyncio .
Conclusion
Getting JSON data from a URL is a common task in Python, and there are several libraries available for this purpose. In this article, we have explored three popular libraries for making HTTP requests: urllib , requests , and aiohttp .
urllib is built-in and suitable for simpler requests or legacy code, while requests offers a more user-friendly and robust interface. aiohttp is optimized for high-performance asynchronous apps and scalability, and is particularly useful for applications that need to make a large number of HTTP requests or require faster response times.
Retrieving data from api and storing in Json file (Python)
![]()
A simple project to store data from api in json file.
- Create a empty python file and name as you wish (Eg: datastore.py ).
- Now lets start importing the modules:
- urllib.request is a Python module for fetching URLs (Uniform Resource Locators) or it defines functions and classes which help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more.
- Now search for any free api to reterive data and here are few free api. search for resources and you will find different few data
-
to get the news details
-
you will get Weather data
-
other free links
- Get the api url for retrieving the data and paste it in any variable.
- Now request get from api url using urlopen() this is to open the URL url, which can be either a string or a Request object.
- Now we need to read the data and decode it into UTF-8 (UTF-8 is a variable-width character encoding used for electronic communication.)
- Create a empty json file in you dir (Eg: data.json ).
- Now we need to open the file and write the data into it.
- open() returns a file object, write() is to write the data into it.
"w" — Write — will overwrite any existing content - When you open json file you will see the data.
When you want to store the specific data.
- Now after the decoded the response, load the data in another variable using json.loads() this is used convert str data to object.
- After getting the required data convert it into str format. To convert we use json.dumps() used to convert object to str.
- Follow the previous step to open and write the data in the file.
Read data from json file convert into object
- Open the file and read the data and convert to object.
- "r" — Read- reads every thing in the file.
- Now you can play with data.
Thanks for Reading the Article!
Enjoy trying with new things
response.json() – Working with JSON in Python requests

In this tutorial, you’ll learn how to parse a Python requests response as JSON and convert it to a Python dictionary. Whenever the requests library is used to make a request, a Response object is returned. The Python requests library provides a helpful method, json(), to convert a Response object to a Python dictionary.
By the end of this tutorial, you’ll have learned:
- How to parse a requests.Response to a Python dictionary using the Response.json() method
- How to convert a Python requests JSON response to a dictionary
- How to handle errors when parsing a Python requests JSON response
- How to convert a Python requests JSON object to a Pandas DataFrame
Table of Contents
Parsing Python requests Response JSON Content
Every request that is made using the Python requests library returns a Response object. This is true for any type of request made, including GET , POST , and PUT requests. The requests library offers a number of different ways to access the content of a response object:
- .content returns the actual content in bytes
- .text returns the content converted to a string, using a character encoding such as UTF-8
Since you’re reading an article about how to parse the JSON from a response into a Python dictionary, the two options above may not be ideal. While you could use the json library to serialize the response, this adds an additional step and complexity to your code.
The requests library comes with a helpful method, .json() , that helps serialize the response of the request. In order to look at an example, let’s the public endpoints provided by the website reqres.in . These endpoints work without signing up, so following along with the tutorial is easy.
Let’s see how we can access the /users endpoint and serialize the response into a Python dictionary using the .json() method:
From the code above, we can see that applying the .json() method to our response created a Python dictionary. In the following section, you’ll learn how to work with the resulting dictionary to access some content.
How to Access Python requests Response Content as a Dictionary
After applying the .json() method to a response created by the requests library, we created a dictionary. This means that we can access the data in the dictionary using common dictionary methods, such as square-bracket indexing or the .get() method.
Let’s see how we can access the ‘page’ key in the data:
In the code above, we applied the .get() method to access the value corresponding with the key ‘page’ . Using the .get() method is a safer way of handling this operation. This is because the method will simply return None , if a key doesn’t exist.
How to Handle Errors When Parsing a JSON Response from Python requests
In some cases, the data that’s returned from a Response object can’t be serialized. In these cases, you’ll encounter a JSONDecodeError . You can safely handle these errors by wrapping your request in a try-except block.
Let’s see how we can safely handle a JSONDecodeError using the requests library:
How to Pretty Print a JSON Object From Python requests
In this section, we’ll take a look at how to pretty print a JSON object that is returned from using the Python requests library. Pretty printing a JSON file is made easy using the json.dumps() function.
Let’s see how we can pretty print a JSON object from the Python requests library:
Let’s break down what we did in the code above:
- We loaded the response the GET request
- We serialized the response using the .json() method
- We then used the dumps() function with an indent of 4 to pretty print the response
How to Convert a Python requests JSON Object to a Pandas DataFrame
In this final section, we’ll take a look at how to convert a requests.Reponse object into a Pandas DataFrame. Because Pandas allows you to easily create DataFrames from a list of dictionaries, we can simply pass in part of our serialized response.
From the code above, we know that the data are stored in a key named ‘data’ as a list of dictionaries. We can easily pass this dictionary into a Pandas DataFrame constructor and convert it to a DataFrame:
In the code above, we serialized our response to a dictionary. From there, we passed the values of the ‘data’ key into the pd.DataFrame constructor, which accepts a list of dictionaries.
Conclusion
In this tutorial, you learned how to use the Python requests library’s Response object and serialize the JSON data using the .json() method. You first learned how to use the .json() method. Then, you learned how to access data from the resulting dictionary. You also learned how to handle errors caused by this method, how to pretty print the resulting data, and how to load it into a Pandas DataFrame.