xml.sax 筆記
阿新 • • 發佈:2018-11-22
from xml.sax import saxutils html_str = """<!DOCTYPE html> <html> <head> <title>name</title> </head> <body> <h1>namejr</h1> <p>my name is namejr</p> <span>my age is <b>22</b> years old</span> <p>other string, such as "*", "@"</p> </body> </html>""" # xml.sax.saxutils.escape(data[, entities={}])將html程式碼進行轉義 # xml.sax.saxutils.escape(data) # 按照規定的進行轉義,轉義的內容:"<"/">"/"&" 對應"<"/">"/"&" print(saxutils.escape(html_str)) """ D:\筆記\python電子書\Python3>python index.py <!DOCTYPE html> <html> <head> <title>name</title> </head> <body> <h1>namejr</h1> <p>my name is namejr</p> <span>my age is <b>22</b> years old</span> <p>other string, such as "*", "@"</p> </body> </html>""" # 如果想轉義自己定義的字元就要使用到entities引數 entities = {'*':'不知道寫啥', '@':'更不知道寫啥'} print(saxutils.escape(html_str, entities=entities)) """ D:\筆記\python電子書\Python3>python index.py <!DOCTYPE html> ... <span>my age is <b>22</b> years old</span> <p>other string, such as "不知道寫啥", "更不知道寫啥"</p> </body> </html>""" # xml.sax.saxutils.unescape() # 與xml.sax.saxutils.escape()相反
xml.sax.saxutils.quoteattr()
from xml.sax import saxutils # xml.sax.saxutils.quoteattr() 與escape()相似,區別是quoteattr()將根據資料的內容選擇引號,試圖避免對字串中的任何引號進行編碼(只使用一種引號不編碼,如果單雙引號都在使用,會對雙引號進行編碼) html_str = "<element attr={}>".format(saxutils.quoteattr("ab' cd\"ef")) print(html_str)