Using the Requests Library in Python
First things first, let’s introduce you to Requests.
What is the Requests Resource?
Requests is an Apache2 Licensed HTTP library, written in Python. It is designed to be used by humans to interact with the language. This means you don’t have to manually add query strings to URLs, or form-encode your POST data. Don’t worry if that made no sense to you. It will in due time.
What can Requests do?
Requests will allow you to send HTTP/1.1 requests using Python. With it, you can add content like headers, form data, multipart files, and parameters via simple Python libraries. It also allows you to access the response data of Python in the same way.
In programming, a library is a collection or pre-configured selection of routines, functions, and operations that a program can use. These elements are often referred to as modules and stored in object format.
Libraries are important because you load a module and take advantage of everything it offers without explicitly linking to every program that relies on them. They are truly standalone, so you can build your own programs with them and yet they remain separate from other programs.
Think of modules as a sort of code template.
To reiterate, Requests is a Python library.
Importing the Requests Module
To work with the Requests library in Python, you must import the appropriate module. You can do this simply by adding the following code at the beginning of your script:
import requests
Of course, to do any of this – installing the library included – you need to download the necessary package first and have it accessible to the interpreter.
Making a Request
When you ping a website or portal for information this is called making a request. That is exactly what the Requests library has been designed to do.
To get a webpage you would do something like the following:
r = requests.get('https://api.github.com/repos/toddmotto/angular-1-5-components-app')
finalResult = r.json()
print "stargazers_count",finalresult["stargazers_count"]