xml.sax 笔记
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)
xml.sax 笔记的更多相关文章
- sax方式解析XML学习笔记
原理:对文档进行顺序扫描,当扫描到文档(document)开始与结束,元素开始与结束.文档结束等地方 通知事件处理函数,由事件处理函数相应动作然后继续同样的扫描,直至文档结束. 优点:消耗资源比较少: ...
- day0203 XML 学习笔记
day02, 03 1. xml语言和作用 2. xml语法详解 2.1 xml 语法声明 2.1.1 encoding 属性 2.1.2 standalone 属性 2.2 xml 元素(Eleme ...
- SpringMVC org.xml.sax.SAXParseException: cvc-complex-type.2.4.c 报错处理方式
使用SpringMVC的过程中需要访问静态文件,即在spring-servlet.xml文件中添加了 <mvc:default-servlet-handler /> 标签,以开启Sprin ...
- org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允许有内容。
二月 25, 2016 9:24:24 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRul ...
- XML学习笔记
XML学习笔记 第一部分:XML简介 我们经常可以听到XML.HTML.XHTML这些语言,后两者比较清楚,一直不是很明白XML是什么,这里做一个总结. XML(eXtensible Markup L ...
- mybatis-config.xml简单笔记
mybatis-config.xml简单笔记 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...
- applicationContext.xml简单笔记
applicationContext.xml简单笔记 <?xml version="1.0" encoding="UTF-8"?> <bean ...
- nested exception is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 56; cvc-complex-type.2.4.c通配符的匹配很全面, 但无法找到元素 'dubbo:application' 的声明
严重: Exception sending context initialized event to listener instance of class org.springframework.we ...
- XML学习笔记(2)--dom4j操作XML
1. 介绍(四种方式的比较这部分转载自:http://www.blogjava.net/xcp/archive/2010/02/12/312617.html) 1)DOM(JAXP Crimson解析 ...
随机推荐
- HTC脚本介绍和入门示例
一.简介 HTC脚本全称是Html Companent即html组件,个人认为它是为了在开发动态HTML中实现代码重用和页面共享目的,主要是把“行为”作为组件封装,可以在很大程度上简化DHTML的开发 ...
- display_errors","On");和error_reporting 区别和联系
ini_set("display_errors","On");和error_reporting(E_ALL); 在用php做网站开发的时候 , 为防止用户看 ...
- HDU 6060 17多校3 RXD and dividing(树+dfs)
Problem Description RXD has a tree T, with the size of n. Each edge has a cost.Define f(S) as the th ...
- 行为参数化和Lambda表达式
行为参数化是指拿出一个代码块把他准备好却不执行它.这个代码块以后可以被程序的其他部分调用,意味着你可以推迟这块代码的执行.方法接受多种行为作为参数,并在内部使用来完成不同的行为.行为参数话的好处在于可 ...
- LOD,听起来很牛逼的样子
<!DOCTYPE html> <html lang="en"> <head> <title>three.js webgl - le ...
- 【转载】 强化学习(三)用动态规划(DP)求解
原文地址: https://www.cnblogs.com/pinard/p/9463815.html ------------------------------------------------ ...
- Python之路PythonNet,第四篇,网络4
pythonnet 网络4 select 支持水平触发 poll 支持水平触发 epoll epoll 也是一种IO多路复用的方式,效率比select和poll 要高一点: epol ...
- Codeforces Div3 #501 A-E(2) F以后补
感觉自己有点强迫症 不都写出来就找理由不写题解 http://codeforces.com/contest/1015 题目链接 A. Points in Segments 题目意思 n个线段 ...
- SpringBoot企业级博客开发
1.springboot生成项目 PS : 进入项目,输入gradle build,生成build文件夹: 然后进入libs有jar,使用java jar进行运行项目 PS: 这个项目没有准守res ...
- linux日志管理
//有关当前登录用户的信息记录在文件utmp中 //登录进入和退出纪录在文件wtmp中 [root@bogon python]# who //who命令查询utmp文件并报告当前登录的每个用户 /va ...