forked from flashreads/blogs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request flashreads#143 from javibodas/python-download-web-…
…page Python download web page
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
id: 030requests-library.md | ||
title: Requests library in Python | ||
tags: | ||
- python | ||
author: Javier Gonzalez | ||
meta-description: | ||
date: 2021-10-08 16:30:00 -0700 | ||
keywords: python | ||
template: post | ||
categories: | ||
- python | ||
cover: | ||
--- | ||
|
||
## Python Requests Library | ||
|
||
Sometimes we need with a script written in Python accessing to an API or download the content of a web page | ||
|
||
For that we can use the [Requests Library](https://docs.python-requests.org/en/latest) | ||
|
||
First we need to install the library: | ||
|
||
```console | ||
pip install requests | ||
``` | ||
|
||
Code Example : | ||
|
||
```python | ||
|
||
import requests | ||
|
||
# The requests module provides us different methods (get, post, update, delete) | ||
r = requests.get('http://www.google.com') | ||
|
||
# In the request method also it is possible to specify the headers, the cookies and the data sent | ||
headers = { 'accept' : '*/*' } | ||
payload = { 'title' : 'Download web page with python' } | ||
cookies = dict(example_cookie='value') | ||
|
||
r = requests.post('https://reqres.in/api/posts', data=payload, headers=headers, cookies=cookies) | ||
|
||
# With the object returned | ||
# We could check the status code of the response | ||
r.status_code | ||
|
||
# We could obtain the content of the page as a string | ||
r.text | ||
|
||
# Or if the url is an API, we could obtain the response in json format | ||
r.json() | ||
|
||
|
||
``` |