Get Webpage with python requests

Python’s requests library is one of the gold standard apis, designed by Kenneth Reitz. It was designed with the user perspective in mind first and implementation second. I have heard this called readme driven development, where the interface the user …


This content originally appeared on DEV Community and was authored by Waylon Walker

Python's requests library is one of the gold standard apis, designed by Kenneth Reitz. It was designed with the user perspective in mind first and implementation second. I have heard this called readme driven development, where the interface the user will use is laid out first, then implemented. This makes the library much mor intuitive than if it were designed around how it was easiest to implement.

Install Requests

Requests is on pypi and can be installed into your virtual environtment with pip.

python -m pip install requests

Getting the content of a request

Requests makes getting content from a web url as easy as possible.

import requests

r = requests.get('https://waylonwalker.com/til/htmx-get/') 
article = r.content

requests is not limited to html

Requests can handle any web request and is not limited to only html. Here are some examples to get a markdown file, a csv, and a png image.

htmx_get_md = requests.get('https://waylonwalker.com/til/htmx-get.md').content
cars = requests.get('https://waylonwalker.com/cars.csv').content 
profile = requests.get('https://images.waylonwalker.com/8bitc.png').content

RTFM

There is way more to requests, this just scratches the surface while covering what you are going to need to get going. The requests docs have way more details.


This content originally appeared on DEV Community and was authored by Waylon Walker


Print Share Comment Cite Upload Translate Updates
APA

Waylon Walker | Sciencx (2022-03-27T02:09:54+00:00) Get Webpage with python requests. Retrieved from https://www.scien.cx/2022/03/27/get-webpage-with-python-requests/

MLA
" » Get Webpage with python requests." Waylon Walker | Sciencx - Sunday March 27, 2022, https://www.scien.cx/2022/03/27/get-webpage-with-python-requests/
HARVARD
Waylon Walker | Sciencx Sunday March 27, 2022 » Get Webpage with python requests., viewed ,<https://www.scien.cx/2022/03/27/get-webpage-with-python-requests/>
VANCOUVER
Waylon Walker | Sciencx - » Get Webpage with python requests. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/27/get-webpage-with-python-requests/
CHICAGO
" » Get Webpage with python requests." Waylon Walker | Sciencx - Accessed . https://www.scien.cx/2022/03/27/get-webpage-with-python-requests/
IEEE
" » Get Webpage with python requests." Waylon Walker | Sciencx [Online]. Available: https://www.scien.cx/2022/03/27/get-webpage-with-python-requests/. [Accessed: ]
rf:citation
» Get Webpage with python requests | Waylon Walker | Sciencx | https://www.scien.cx/2022/03/27/get-webpage-with-python-requests/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.