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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course