How python processes html Escape characters
This document describes how to process html Escape characters in python. 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.