How to Check Backlink in Python | Reading URL Page and Status Code
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.
How to read the data from internet URL?
You can use the
requestsmodule in your Python program.
get()method from the
requestsmodule to the request data by passing the web page URL as an attribute.
- Use the
textattribute 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)
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.
How to read the status code of the URL?
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.
- 200- if the request is successfully executed (when you provide valid URL)
- 404- if the request URL is not valid.
For status code, use
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) print("URL status code", data)
- If you give the valid URL path, it will return the 200 status code.
- If you give the invalid URL path, it will return the 404 “file not found” status code.
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.
How to check backlink in Python? (Backlink Checker Tool)
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.
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.")
As the given backlink is present in the URL web page,
check_backlink() will return a positive integer value.
So the output is,
Automating Boring Stuff
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.
- How to find backlinks to a specific page?
Read the data from the given page and list down all the backlinks.
- Develop A tool to extract all the email addresses from the web page URL.
Read the data from the given page and find all the email addresses in the data.
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.