×

How to Read html page in python

Html is a Hyper Text Markup language is a standard language used for creating webpages. HTML is the name of the language used to describe the construction of Web pages. HTML was initially created to define the goal of defining the structure of documents, such as headings, paragraphs, lists, and other elements, to make it easier for researchers to share scientific data.

Web browsers download HTML documents from a web server or local storage, converting them into multimedia web pages. HTML's initial iteration included visual cues for the document's appearance and semantic descriptions of a web page's structure.

Reading the HTML Page

1) Requests. get(url)

Import the Python library requests, which manages the server-side details of requesting websites in a simple format that is simple to use. To access the website, call requests. Get (...) and pass the argument to the URL "https://google.com" so the function knows where to go. Look at the get request's actual body (the return value is a request object containing useful meta information like the file type, etc.).

Example

import requests
print(requests.get(url = 'https://google.com').text)

2) urllib.request:

The urllib. Request () function is a suggested method for obtaining web resources from a website. This also works to make a straightforward Python 3 one-liner to access the Google website as before:

Example

import urllib.request as r
page = r.urlopen('https://google.com')
print(page.read())

Example: “Hello World” html page in python:

The concept that a file that appears to contain code from one angle can be viewed as data from another is one of the more potent ones in computer science. Creating programs that control other programs is thus feasible. We'll use Python to produce a "Hello World!"-themed HTML file next. To accomplish this, we'll save the HTML tags in a multiline Python string and create a new file from its contents. The.html extension will be used to save this document rather than the.txt extension.

Normally, a doctype declaration is the first line of an HTML file. This was demonstrated in a previous lesson when you created an HTML "Hello World" program. We will leave out the doctype in this example to make it simpler to read our code. Remember that the text is encased in three quotation marks to create a multi-line string?

The below example shows the html page in python:

# write-html.py


f = open('helloworld.html','w')


message = """<html>
<head></head>
<body><p>Hello World!</p></body>
</html>"""


f.write(message)
f.close()

Go to your Firefox browser, select File -> New Tab, then select File -> Open File on the tab. Choose helloworld.html. Your message should now be visible in the browser. Consider the moment that you can now create a program that will automatically create a webpage. There is no reason why, if you wanted to, you couldn't write a program that would instantly build an entire website.

Using Os and Webbrowser

The Python webbrowser module provides a high-level interface for displaying Web-based documents, the user's high-level interface for displaying Web-based documents to users is provided by the Python webbrowser module, and the OS module provides a portable method of accessing operating system-dependent functionality. The webbrowser module offers a high-level interface that lets users view documents users to view documents that are hosted on the Web. In most cases, simply calling this module's open() function will open a URL in the default browser. The module must be imported, and the open() function must be used.

According to the overview, dynamic web applications usually involve gathering data from a web page form, processing it in a server-side program, and displaying the results on a web page.

This section uses familiar keyboard input into a typical Python program before processing the input and producing the final web page output, just like in the final version, because introducing all these new concepts at once might be difficult for the reader to understand. Let's look at how using these two together can help us open an HTML page in the Chrome browser: 

Used function: Use the open new tab feature of your default browser to open an HTML file in a new tab ()

Syntax

open_new_tab(filename)

Example

import webbrowser
import os
f = open('GFG.html', 'w')
html_template = """
<html>
<head></head>
<body>
<p>HTML PAGE IN PYTHON</p>
</body>
</html>
"""
f.write(html_template)
f.close()
filename = 'file:///'+os.getcwd()+'/' + 'WP.html'
webbrowser.open_new_tab(filename)

Output

HTML PAGE IN PYTHON


Related Topics

Python hash() function

Python hash() function The hash() function in Python returns the hash value of the object (if it has one).  Syntax hash(object) Parameter obj : This parameter represents the object which we need to convert into hash. Return This...

1 minute read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.

How to Sort a String in Python?

The characters in the string are sorted or put in alphabetical order using the sort string function in Python. Python has built-in techniques for sorting strings available. Since we occasionally...

6 minutes read.

Cx_Oracle Python with Example

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python PyMOTW

The cmd module comprises one class of the general public, Cmd, meant for command processors such interactive shells and other command interpreters as a foundation class. It utilises readline as...

3 minutes read.

Python Pass Statement

The pass statement is a null statement. The difference between pass and comment is that comment is ignored by the interpreter, whereas pass is not. The pass statement is typically used...

3 minutes read.

Python lambda() Function

In this tutorial, we will learn about a new concept known as the Lambda function. It is a relatively new concept while learning Python. Python lambda functions are anonymous functions. It...

3 minutes read.

Python Dictionary update() method

Python Dictionary update() method The dictionary.update() method in Python inserts the specified items to the dictionary. Syntax dictionary.update(iterable) Parameter iterable- This parameter represents a dictionary or an iterable object with key value pairs, that will...

1 minute read.

Python math.cos and math.acos function

Math.cos() function In Python, the Math module is used for performing the mathematical operations. It includes the math.cos() function that is used for obtaining the cosine value of an angle in...

3 minutes read.

Python BytesIO

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python Unit Test Cheat String

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python vars() Function

Python vars() Function The vars() function in Python returns the __dict__ attribute for a module, class, instance, or any other object with a __dict__ attribute. Syntax vars([object]) Parameter object: This parameter represents any object with a __dict__attribute Return This function returns...

1 minute read.

List Assignment Index out of Range in Python

As we know, a List is one of the four unique data structures available in Python; In this tutorial, we will deep dive into understanding iterating through a list and...

3 minutes read.

How to make a firewall in Python?

Firewall: The firewall is a network which controls the incoming and outgoing network traffics of a monitor. It blocks the dataset based on the set of rules written in the security...

3 minutes read.

Python String ljust() method

Python String ljust() method The string.ljust() method in Python will left align the string, where the padding is done using the parameter fillchar (space is default). Syntax String.ljust(width[, fillchar]) Parameter width: This parameter represents the...

2 minutes read.

Convert XML to JSON in Python

XML conversion is very useful if we work on an API that returns data in JSON format and the source of data is in XML format. JSON A JSON file reserves the...

4 minutes read.

Difference between python list and tuple

In this tutorial, we will understand the key differences between python lists and tuples in python. Firstly, let us see the similarities between Lists and Tuples. Both are two data types...

4 minutes read.

How to reverse a string in Python

In Python language, we have a few ways to reverse a string. In this article, let us discuss these ways and understand the suitability of these ways in different scenarios...

5 minutes read.

Difference between Package and Module in Python

What are Python modules? A file with the “.py” suffix that includes Python or C executable code is known as a module. Multiple Python commands and expressions make compose a module....

3 minutes read.

Is Python Object Oriented Programming language

In this tutorial, we will see whether python also belongs to an object-oriented programming language like several others. We will also see the advantages of using OOPs. Further, we will...

4 minutes read.