This article mainly introduces how to process html escape characters using python, and analyzes in detail the processing skills of common HTML escape characters using examples, for more information about how to process html escape characters in python, see the following example. We will share this with you for your reference. The details are as follows:
Recently, when using Python to process webpage data, we often encounter html escape characters (also called html character entities), such as <>. Character entities are generally used to indicate reserved characters in the web page, for example,> use> to prevent the browser from being considered a tag. for details, refer to the HTML character entity of w3school. Although useful, they will greatly affect the parsing of webpage data. To process these escape characters, the following solutions are available:
1. use HTMLParser for processing
import HTMLParserhtml_cont = " asdfg>123<"html_parser = HTMLParser.HTMLParser()new_cont = html_parser.unescape(html_cont)print new_cont #new_cont = " asdfg>123<"
Convert back (only space cannot be converted back ):
import cginew_cont = cgi.escape(new_cont)print new_cont #new_cont = " asdfg>123<"
2. directly replace one by one
html_cont = " asdfg>123<"new_cont = new_cont.replace(' ', ' ')print new_cont #new_cont = " asdfg>123<"new_cont = new_cont.replace('>', '>')print new_cont #new_cont = " asdfg>123<"new_cont = new_cont.replace('<', '<')print new_cont #new_cont = " asdfg>123<"
I don't know if there is any better way.
In addition, stackoverflow provides a solution for handling escape characters in xml: python-What's the best way to handle-like entities in XML documents Ents with lxml? -Stack Overflow.
For more articles about how to use python to process html escape characters, refer to the Chinese PHP website!