We will start with the basic of the reading URL data from the internet and HTTP status code.
After that, we will see how you can check backlink in Python- whether a given backlink is present or not in a web page URL.
Let’s start digging. Python is amazing.
Table of Contents
You can use the requests
module.
requests
module in your Python program.get()
method from the requests
module to the request data by passing the web page URL as an attribute.text
attribute to get URL page text data.Here in this example. I’m using Python Wikipedia URL for demonstration.
import requests
def get_url_data(link):
f = requests.get(link)
return f.text
if __name__=="__main__":
url = "https://en.wikipedia.org/wiki/Python_(programming_language)"
data = get_url_data(url)
print(data)
Output:
It will print the complete HTML data content from Wikipedia web page.
You can make this script automated by taking the URL as user input in Python.
Whenever you open any web URL, it returns the HTTP status code along with the HTML data.
In this case, we are considering two HTTP status code.
For status code, use status_code
attribute.
We are extending the above program to get web page text data and the status response code.
import requests
def get_url_data(link):
f = requests.get(link)
return f.text, f.status_code
if __name__=="__main__":
url = "https://en.wikipedia.org/wiki/Python_(programming_language)"
data = get_url_data(url)
print("URL data", data[0])
print("URL status code", data[1])
Output:
I was working on one of the Python Django projects for developing the online SEO (Search Engine Optimization) tool. In that project, I had to check whether a particular link (backlink) is present in the given web page URL or not.
We are writing check_backlink()
function which will return -1 if the backlink is not present in the given URL. Otherwise, it returns a positive integer that is an index of the first matching backlink.
Note: find()
is a string method that returns the index of the first matching string. If the given string is not present, it returns -1.
Based on the response from the check_backlink()
method, an appropriate message is printed using the if-else statement.
Here is the simple program to check the backlink.
Simple Python Backlink Checker Tool
import requests
def check_backlink(url, backlink):
f = requests.get(url)
data = f.text
return data.find(backlink)
if __name__=="__main__":
url="https://en.wikipedia.org/wiki/Python_(programming_language)"
backlink="https://www.python.org/downloads/release/python-383/"
res = check_backlink(url, backlink)
if res == -1:
print("Backlink not found.")
else:
print("Backlink found.")
Output:
As the given backlink is present in the URL web page, check_backlink()
will return a positive integer value.
So the output is,
Backlink found.
Just like checking backlink, there are many use cases you can automate where you can use the requests
module to read the data from the internet.
Here are some of the use cases, you can try out. In other words, these are the ideas to develop the tool and to automate some of the boring stuff.
Why do I like Python so much?
With very few line of code, we can do some much and automating some boring stuff.
In this Python tutorial, we have learned reading data from the URL, checking status code, and to check backlink in Python.
Any questions? Feel free to ask in the comment section.
This is a really helpful post sir thanks for sharing your knowledge.
Thanks for this article sir.
You’re welcome!