lxml installation –
It is a Python binding for C libraries – libxslt and libxml2. So maintaining a Python base, it is very fast HTML parsing and XML library. To let it work – C libraries also need to be installed. The link – http://lxml.de/installation.html will provide all the installation instructions.
sudo apt-get install python-lxml or pip install lxml
Cleaning task is performed using
clean_html() function present in the
lxml.html.clean module. This function removes the unnecessary HTML tags. In the code below,
clean_html() function in the
As you can see that the results are much easier and cleaner. Thus, makes our job easy to deal with the HTML.
Converting HTML Entities –
Strings such as “
&” or “
<” are HTML entities. These are normal ASCII character encoding having special uses in HTML. “
<” is the entity for “
"<" is present within HTML tags and it is the beginning character for an HTML tag. So, to escape it
"<" entity is defined.
"&" is entity code for
To process the text within an HTML document, convert these entities back to their normal characters so as to recognize them and use them appropriately.
1) install BeautifulSoup
2) sudo easy_install beautifulsoup4 or sudo pip install beautifulsoup4
It is an HTML parser library used for entity conversion. It simply creates an instance of BeautifulSoup given a string containing HTML entities. And then it retrieves the string attribute:
But the reverse for it is not possible i.e. for ‘<' in BeautifulSoup, a None result is obtained as it is invalid in HTML. BeautifulSoup looks for tokens that look similar to an entity and in order to convert the HTML entities, it replaces them with their corresponding value in the
htmlentitydefs.name2codepoint dictionary which is there in the python standard library.
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
- Time Functions in Python | Set-2 (Date Manipulations)
- Send mail from your Gmail account using Python
- Python – The new generation Language
- Print Single and Multiple variable in Python
- Increment and Decrement Operators in Python
- str() vs repr() in Python
- Swap two variables in one line in C/C++, Python, PHP and Java
- Generate all permutation of a set in Python
- Class or Static Variables in Python
- trunc() in Python
- Division Operators in Python
- Interesting facts about strings in Python | Set 1
- When to use yield instead of return in Python?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.