From 09a72f0b6eb34ea4da00627fe4fdd6333a914395 Mon Sep 17 00:00:00 2001 From: Javier Gonzalez Date: Sat, 9 Oct 2021 17:06:22 +0200 Subject: [PATCH 1/2] Added post for using requests library of python --- python/030requests-library.md | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 python/030requests-library.md diff --git a/python/030requests-library.md b/python/030requests-library.md new file mode 100644 index 0000000..e955f4d --- /dev/null +++ b/python/030requests-library.md @@ -0,0 +1,49 @@ +--- +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) + +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() + + +``` From ab275414108347b18740fa45968d5116b1bd6c95 Mon Sep 17 00:00:00 2001 From: Javier Gonzalez Date: Sat, 9 Oct 2021 17:09:23 +0200 Subject: [PATCH 2/2] Added installation of library --- python/030requests-library.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/python/030requests-library.md b/python/030requests-library.md index e955f4d..15c00f3 100644 --- a/python/030requests-library.md +++ b/python/030requests-library.md @@ -19,6 +19,12 @@ Sometimes we need with a script written in Python accessing to an API or downloa 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