Navigation X
ALERT
Click here to register with a few steps and explore all our cool stuff we have to offer!

cracked.io | Best Forum Around | Free Premium Accounts




 3229

[sunjester tuts] HTTP GET/Web Requests with Python

by sunjester - 13 March, 2019 - 12:16 AM
This post is by a banned member (sunjester) - Unhide
sunjester  
Registered
18
Posts
12
Threads
5 Years of service
#1
Introduction
I normally don't use python very much. However, I can always jump right into using it since I know how to properly read documentation. I am going to show you how you can read some documentation and start making requests to websites through python and the requests library. As with all my tutorials we will be using Linux (ubuntu xenial), and no I will not cater to the Windows kids.
 
Requirements
This tutorial will require a few things, including python. I am using 2.7 (most of you probably use 3, I don't.).
  • Python >= 2.7
  • pip >= 19.03
  • python requests library >= 2.21
Checking Versions with Pip
You can easily check the requests version, or if it's even installed like this
Code:
pip show requests
[Image: jJ4r3pM.png]
 
Documentation
The website for the requests library is found here. We will be following the documentation and using the software as intended by the developers, which should give you an insight into reading documentation and finding/using other libraries.
 
Including the Library
It's pretty easy to add the library to your script. Like almost all other libraries, using the import keyword.
Code:
import requests
Getting HTML
We can use a simple line of code to requests an HTTP GET request for HTML of a web site.
Code:
req = requests.get("https://www.imdb.com/title/tt0093177/") 
This is where reading the documentation will come in handy. The code above will make the request to the IMDB page, but printing the req variable probably won't give you what you are looking for, so let's see what the will do.
 
[Image: uCu4rib.png]
 
It printed the response code. Back on the website (documentation) we see that it's a response object, which is probably why it has the angle brackets around it also.
 
[Image: OkAqpim.png]
 
Response Object Documentation
Since it returned an object, let's go see what the properties of this object are in the documentation. Here is the documentation for the Response object of the Requests library. We can see there are a few things here, let's try to use content and see if we get the source.
Code:
#!/usr/bin/env python
import requests

req = requests.get("https://www.imdb.com/title/tt0093177/") 
print req.content
[Image: Je65nId.png]
 
Yep, there is the source.
This post is by a banned member (nsarat) - Unhide
nsarat  
Registered
4
Posts
0
Threads
5 Years of service
#2
thanks ive always wanted to get into python
This post is by a banned member (socalgurl) - Unhide
socalgurl  
Registered
10
Posts
0
Threads
5 Years of service
#3
im glad this is here
This post is by a banned member (samirtitraoui) - Unhide
8
Posts
0
Threads
4 Years of service
#4
thanks

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
or
Sign in
Already have an account? Sign in here.


Forum Jump:


Users browsing this thread: 1 Guest(s)