https://github.com/oxylabs/python-requests
Learn how to use Python Requests module
https://github.com/oxylabs/python-requests
get-request-python github-python http-client json python python-ecommerce python-library python-web-crawler requests scraper-python serp-api-python
Last synced: 3 months ago
JSON representation
Learn how to use Python Requests module
- Host: GitHub
- URL: https://github.com/oxylabs/python-requests
- Owner: oxylabs
- Created: 2022-10-19T11:39:24.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-19T10:51:02.000Z (about 1 year ago)
- Last Synced: 2025-01-17T21:07:25.861Z (5 months ago)
- Topics: get-request-python, github-python, http-client, json, python, python-ecommerce, python-library, python-web-crawler, requests, scraper-python, serp-api-python
- Language: Python
- Homepage:
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Python Requests Library
[](https://oxylabs.go2cloud.org/aff_c?offer_id=7&aff_id=877&url_id=112)
[](https://discord.gg/GbxmdGhZjq)
[
](https://github.com/topics/requests) [
](https://github.com/topics/python)
- [Getting started with Requests](#getting-started-with-requests)
- [Python requests: GET](#python-requests-get)
- [Reading responses](#reading-responses)
- [Using Python request headers](#using-python-request-headers)
- [Python requests: POST](#python-requests-post)The **Requests** module is one widely used to send HTTP requests. It’s a third-party alternative to the standard [urllib](https://docs.python.org/3/library/urllib.html), [urllib2](https://docs.python.org/2/library/urllib2.html), and [urllib3](https://urllib3.readthedocs.io/en/latest/) as they can be confusing and often need to be used together. **Requests** in Python greatly simplifies sending HTTP requests to their destination.
This article gives you an overview of everything you need about Requests.
For a detailed explanation, see our [blog post](https://oxylabs.io/blog/python-requests).
## Getting started with Requests
Installing Requests is simple as it can be done through a terminal.
```shell
$ pip install requests
```Finally, before beginning to use Requests in any project, the library needs to be imported:
```python
import requests
```## Python requests: GET
To send a GET request, invoke `requests.get()` in Python and add a destination URL, as follows:
```python
import requests
requests.get('http://httpbin.org/')
```GET requests can be sent with specific parameters if required:
```python
payload = {'key1': 'value1', 'key2': 'value2'}
requests.get('http://httpbin.org/', params=payload)
```## Reading responses
```python
response = requests.get('http://httpbin.org/')
print(response.status_code)# OUTPUT:
```To read the content of the response, we need to access the text part by using `response.text`:
```python
print(response.text)
```Responses can also be decoded to the JSON format:
```python
response = requests.get('http://api.github.com')
print(response.json())
```## Using Python request headers
Response headers are another important part of the request:
```python
print(response.headers)
```You can also send custom Python request headers. To check whether our request header has been sent successfully, we will need to make the call `response.request.headers`:
```python
import requestsheaders = {'user-agent': 'my-agent/1.0.1'}
response = requests.get('http://httpbin.org/', headers=headers)
print(response.request.headers)
```## Python requests: POST
Sending a POST request is *almost* as simple as sending a GET:
```python
response = requests.post('https://httpbin.org/post', data = {'key':'value'})
```The requests library accepts arguments from dictionary objects which can be utilized to send more advanced data:
```python
payload = {'key1': 'value1', 'key2': 'value2'}
response = requests.post('https://httpbin.org/post', data = payload)
```Requests have an added feature that automatically converts the POST request data into JSON.
```python
import requestspayload = {'key1': 'value1', 'key2': 'value2'}
response = requests.post('https://httpbin.org/post', json = payload)
print(response.json())
```Alternatively, the *json* library might be used to convert dictionaries into JSON objects:
```python
import json
import requestspayload = {
'key1': 'value1',
'key2': 'value2'}
jsonData = json.dumps(payload)
response = requests.post('https://httpbin.org/post', data = jsonData)
print(response.json())
```If you wish to learn more about Requests, see our [blog post](https://oxylabs.io/blog/python-requests).