• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2021
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools

How to Check Backlink in Python | Reading URL Page and Status Code

Aniruddha Chaudhari/5706/2
CodePython
YouTube CSEstack

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

  • How to read the data from internet URL?
  • How to read the status code of the URL?
  • How to check backlink in Python? (Backlink Checker Tool)
  • Automating Boring Stuff

How to read the data from internet URL?

You can use the requests module.

  • Import requests module in your Python program.
  • Use get() method from the requests module to the request data by passing the web page URL as an attribute.
  • Use the 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.

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 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:

  • 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.

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.

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.

Python Interview Questions eBook

Python
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Smart Rahuljackson
    August 14, 2020 at 9:55 pm

    This is a really helpful post sir thanks for sharing your knowledge.

    Thanks for this article sir.

    • Reply
      Aniruddha Chaudhari
      August 15, 2020 at 9:55 pm

      You’re welcome!

Leave a Reply Cancel reply

Basic Python Tutorial

  1. Python- Tutorial Overview
  2. Python- Applications
  3. Python- Setup on Linux
  4. Python- Setup on Windows
  5. Python- Basic Syntax
  6. Python- Variable Declaration
  7. Python- Numeric Data Types
  8. Python- NoneType
  9. Python- if-else/elif
  10. Python- for/while else
  11. Python- User Input
  12. Python- Multiline User Input
  13. Python- String Formatting
  14. Python- Find Substring in String
  15. Python- Bitwise Operators
  16. Python- Range Function
  17. Python- List
  18. Python- List Vs Tuple
  19. Python- Compare Two Lists
  20. Python- Sorting List
  21. Python- Delete Element from List
  22. Python- Dictionary
  23. Python- ‘is’ vs ‘==’
  24. Python- Mutable vs Immutable
  25. Python- Generator & Yield
  26. Python- Fibonacci Generator
  27. Python- Assert Statement
  28. Python- Exception Handling 
  29. Python- RegEx
  30. Python- Lambda Function
  31. Python- Installing Modules
  32. Python- Important Modules
  33. Python- Find all Installed Modules
  34. PyCharm- IDE setup
  35. Python- File Handling
  36. Python- Monkey Patching
  37. Python- Decorators
  38. Python- Instance vs Static vs Class Method
  39. Python- Name Mangling
  40. Python- Working with GUI
  41. Python- Read Data from Web URL
  42. Python- Memory Management
  43. Python- Virtual Environment
  44. Python- Calling C Function

Python Exercise

  1. Python- Tricky Questions
  2. Python- Interview Questions (60+)
  3. Python- Project Ideas (45+)
  4. Python- MCQ Test Online
  5. Python- Coding Questions (50+)
  6. Python- Competitive Coding Questions (20+)

Python String

  1. Reverse the String
  2. Permutations of String
  3. Padding Zeros to String/Number

Python List

  1. Randomly Select Item from List
  2. Find Unique Elements from List
  3. Are all Elements in List Same?

Python Dictionary

  1. Set Default Value in Dictionary
  2. Remove all 0 from a dictionary

File Handling

  1. Python- Read CSV File into List
  2. Check if the File Exist in Python
  3. Find Longest Line from File

Compilation & Byte Code

  1. Multiple Py Versions on System
  2. Convert .py file .pyc file
  3. Disassemble Python Bytecode

Algorithms

  1. Sorting- Selection Sort
  2. Sorting- Quick Sort

Other Python Articles

  1. Clear Py Interpreter Console
  2. Can I build Mobile App in Python?
  3. Extract all the Emails from File
  4. Python Shell Scripting

© 2021 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard