[Python Tips] Requests

avatar

Requests is a powerful and easy to use HTTP library for Python

Need to get HTML data? Need to interact with an API? Requests can do that.

While Python has a built-in library urllib3 for these situations, requests wraps this library with an easy to use interface.

Installation

To install requests, you can use pip to install it.

pip install -U requests

The -U is a good practice to force an upgrade if it is already installed.

I recommend using a virtual environment. You can learn more about them in my previous tutorial on Virtual Environments.

Usage

While requests has a lot of functionality, I am going to cover the basic usage and you can refer to the documentation for the more advanced usage.

Basic Query

import requests
req = requests.get('https://pokeapi.co/api/v2/pokemon/ditto/`)

Status Codes

You can verify you have a proper response by checking the status code.

req.status_code

200 means everything went well, you can look up other codes here.

You can also use this technique to verify a good status code.

if req.status_code == requests.codes.ok:

There is a cleaner way to send a request and check the status code:

import requests
req = req = requests.get('https://pokeapi.co/api/v2/pokemon/ditto/`)

try:
    req.raise_for_status()
except requests.exceptions.HTTPError as e: 
    print e

Data

You can retrieve the response data using req.text, req.json() as bytes with req.content, or raw with req.raw.

Most of the time you will be using req.json() and will be able to use the result as normal json data.

Requests can be used to POST, pass parameters, work with custom headers, and work with form data. If you want to learn other uses of the requests library, check out their documentation.

Requests is a simple by powerful python module that will handle 98% of your HTTP needs.

My Python Tips Series



0
0
0.000
7 comments
avatar

Requests are so absoloutely huge, if you're trying to get your project to speak with a database. I've got one of my projects that links with the running app, Strava.

Great recommendation.

0
0
0.000
avatar

Don't like themarkymark either? This bot downvotes everything themarkymark posts. Help by delegating:

5 | 10 | 20 | 50 | 100 | 500 | 1000

Unvote themarkymark for witness here



"I've never seen anyone leave such a long trail of blazing dumpster fires. I now believe the wreckless downvoting and blacklisting of thousands of accounts by themarkymark et al is the SOLE reason the price of Steem continues to fall. I didn't even make the photo for this post. That's right, I found it online. Fucking amazing."

Look at all these complaints from people all over the Internet

Most of these are written OFF of Steemit so as to avoid being downvoted and hidden. Amazing. So much for this "censorship-free blockchain".

themarkymark Hey. Sorry about your shit coin...

Groups of people and a top witness (themarkymark) are trying to dive me off of steem/steemit by harassing me

Getting blacklisted (by themarkymark) becomes expensive on Steem

themarkymark & Berniesanders Responsible For Altcoin Collapse?

Is TheMarkyMark Abusing The Steemit Blockchain?

https://m.youtube.com/watch?v=3gnH8lxxFQM

Listen as themarkymark evades questions about his identity and gives up his real motives

The Arrogant "themarkymark"

Dedicated to that unknown mystery douche: themarkymark

@themarkymark Stole Thousands Of $$ From Me

My Letter to @themarkymark - in response to his continual flagging of my account

Harassment and Censorship by themarkymark continues. Now flagging everything I post for no reason

Crooket @themarkymark

@themarkymark, another fake account that flags without reason, like the berniesanders whales circle jerk

Another example of censorhsip on steemit. Witness (themarkymark) downvotes truth about his actions.

Big Thanks For ThemarkyMark, Anyx, Patrice and Others Who Helped Ruin Steemit

Open letter to those who provide Red Flag with downvotes

My Post About Asian Countries Being the Largest Ocean Polluters Was Downvoted By Badcontent. WHY?

I Got Downvoted!?! It's Not Fair! Can You Help Me???

More THIEVES (themarkymark) on Steem Blockchain? Be careful with bidbots...

themarkymark_nzi.png

riffraff.png

Clone this protest bot

0
0
0.000
avatar

I enjoy reading your Python-Tips posts. I did notice you did not post in your STEMGeeks community, are you waiting for the communities beta to mature?

Shameless plug, I created a Learning Python community and would appreciate your thoughts.

0
0
0.000
avatar

Don't like themarkymark either? This bot downvotes everything themarkymark posts. Help by delegating:

5 | 10 | 20 | 50 | 100 | 500 | 1000

Unvote themarkymark for witness here



"I've never seen anyone leave such a long trail of blazing dumpster fires. I now believe the wreckless downvoting and blacklisting of thousands of accounts by themarkymark et al is the SOLE reason the price of Steem continues to fall. I didn't even make the photo for this post. That's right, I found it online. Fucking amazing."

Look at all these complaints from people all over the Internet

Most of these are written OFF of Steemit so as to avoid being downvoted and hidden. Amazing. So much for this "censorship-free blockchain".

themarkymark Hey. Sorry about your shit coin...

Groups of people and a top witness (themarkymark) are trying to dive me off of steem/steemit by harassing me

Getting blacklisted (by themarkymark) becomes expensive on Steem

themarkymark & Berniesanders Responsible For Altcoin Collapse?

Is TheMarkyMark Abusing The Steemit Blockchain?

https://m.youtube.com/watch?v=3gnH8lxxFQM

Listen as themarkymark evades questions about his identity and gives up his real motives

The Arrogant "themarkymark"

Dedicated to that unknown mystery douche: themarkymark

@themarkymark Stole Thousands Of $$ From Me

My Letter to @themarkymark - in response to his continual flagging of my account

Harassment and Censorship by themarkymark continues. Now flagging everything I post for no reason

Crooket @themarkymark

@themarkymark, another fake account that flags without reason, like the berniesanders whales circle jerk

Another example of censorhsip on steemit. Witness (themarkymark) downvotes truth about his actions.

Big Thanks For ThemarkyMark, Anyx, Patrice and Others Who Helped Ruin Steemit

Open letter to those who provide Red Flag with downvotes

My Post About Asian Countries Being the Largest Ocean Polluters Was Downvoted By Badcontent. WHY?

I Got Downvoted!?! It's Not Fair! Can You Help Me???

More THIEVES (themarkymark) on Steem Blockchain? Be careful with bidbots...

themarkymark_nzi.png

riffraff.png

Clone this protest bot

0
0
0.000
avatar

Good tips.. nice sharing keep it up...

0
0
0.000
avatar

Don't like themarkymark either? This bot downvotes everything themarkymark posts. Help by delegating:

5 | 10 | 20 | 50 | 100 | 500 | 1000

Unvote themarkymark for witness here



"I've never seen anyone leave such a long trail of blazing dumpster fires. I now believe the wreckless downvoting and blacklisting of thousands of accounts by themarkymark et al is the SOLE reason the price of Steem continues to fall. I didn't even make the photo for this post. That's right, I found it online. Fucking amazing."

Look at all these complaints from people all over the Internet

Most of these are written OFF of Steemit so as to avoid being downvoted and hidden. Amazing. So much for this "censorship-free blockchain".

themarkymark Hey. Sorry about your shit coin...

Groups of people and a top witness (themarkymark) are trying to dive me off of steem/steemit by harassing me

Getting blacklisted (by themarkymark) becomes expensive on Steem

themarkymark & Berniesanders Responsible For Altcoin Collapse?

Is TheMarkyMark Abusing The Steemit Blockchain?

https://m.youtube.com/watch?v=3gnH8lxxFQM

Listen as themarkymark evades questions about his identity and gives up his real motives

The Arrogant "themarkymark"

Dedicated to that unknown mystery douche: themarkymark

@themarkymark Stole Thousands Of $$ From Me

My Letter to @themarkymark - in response to his continual flagging of my account

Harassment and Censorship by themarkymark continues. Now flagging everything I post for no reason

Crooket @themarkymark

@themarkymark, another fake account that flags without reason, like the berniesanders whales circle jerk

Another example of censorhsip on steemit. Witness (themarkymark) downvotes truth about his actions.

Big Thanks For ThemarkyMark, Anyx, Patrice and Others Who Helped Ruin Steemit

Open letter to those who provide Red Flag with downvotes

My Post About Asian Countries Being the Largest Ocean Polluters Was Downvoted By Badcontent. WHY?

I Got Downvoted!?! It's Not Fair! Can You Help Me???

More THIEVES (themarkymark) on Steem Blockchain? Be careful with bidbots...

themarkymark_nzi.png

riffraff.png

Clone this protest bot

0
0
0.000