• 周四. 4月 25th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Python常用模块之xml模块

admin

11月 28, 2021
我们向服务器发送请求,会给我们一个字符串类型的response,这个response可以表现
为HTML标签的样子,访问博客园返回的就是HTML标签,也可以表现为字典或者列表的样子,也可以表现为xml的样子,如图
这是xml的基本格式,一层包着一层

访问天气的时候返回的经常就是json,如图,实际是一个Python的字典


对于XML语言,Python有个专门的xml模块处理这种格式,转化为XML语言
import requests#发送http请求(用py模拟浏览器浏览网页)
from xml.etree import ElementTree#处理xml语言

r = requests.get('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=810833035')
#这个网站验证自己的QQ是否在线,在线会在网页里显示Y
result = r.text#这里返回的是一个xml类型的字符串
print(result)

data =ElementTree.XML(result)#XML接受一个字符串参数,转华为特殊格式,就可以用.text获取内容了


if data.text == 'Y':#可以访问节点里面的信息
    print('你的QQ在线')
else:
    print('你的QQ不在线')

  如图为运行结果

下面展示XML模块更详细的用法,这段xml代码是在博客园点击订阅按钮返回的

<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">博客园_change_4_now</title>
<subtitle type="text">改变为了遇见最好的自己</subtitle>
<id>uuid:8acafdc9-7a82-4367-88fb-2f11c2b3d7fc;id=2799</id>
<updated>2017-02-20T15:32:57Z</updated>
<author>
<name>change_4_now</name>
<uri>http://www.cnblogs.com/change4now/</uri>
</author>
<generator>feed.cnblogs.com</generator>
<entry>
<id>http://www.cnblogs.com/change4now/p/6421850.html</id>
<title type="text">C# Redis学习系列一:Redis的认识、下载、安装、使用 - change_4_now</title>
<summary type="text">
一.认识Redis1. Redis 是一个高性能的key-value数据库。 2. 它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。 3.周期性的把更新的数据写入磁盘或者把修改操作
</summary>
<published>2017-02-20T14:19:00Z</published>
<updated>2017-02-20T14:19:00Z</updated>
<author>
<name>change_4_now</name>
<uri>http://www.cnblogs.com/change4now/</uri>
</author>
<link rel="alternate" href="http://www.cnblogs.com/change4now/p/6421850.html"/>
<link rel="alternate" type="text/html" href="http://www.cnblogs.com/change4now/p/6421850.html"/>
<content type="html">
【摘要】一.认识Redis1. Redis 是一个高性能的key-value数据库。 2. 它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。 3.周期性的把更新的数据写入磁盘或者把修改操作 <a href="http://www.cnblogs.com/change4now/p/6421850.html" target="_blank">阅读全文</a>
</content>
</entry>
<entry>
<id>http://www.cnblogs.com/change4now/p/5840919.html</id>
<title type="text">
关于ADO.NET 实体数据数据模型无法为Mysql 选择6.0 解决方案 - change_4_now
</title>
<summary type="text">您的项目引用了最新实体框架;但是,找不到数据链接所需的与版本兼容的实体框架数据库</summary>
<published>2016-09-04T17:06:00Z</published>
<updated>2016-09-04T17:06:00Z</updated>
<author>
<name>change_4_now</name>
<uri>http://www.cnblogs.com/change4now/</uri>
</author>
<link rel="alternate" href="http://www.cnblogs.com/change4now/p/5840919.html"/>
<link rel="alternate" type="text/html" href="http://www.cnblogs.com/change4now/p/5840919.html"/>
<content type="html">
【摘要】您的项目引用了最新实体框架;但是,找不到数据链接所需的与版本兼容的实体框架数据库 <a href="http://www.cnblogs.com/change4now/p/5840919.html" target="_blank">阅读全文</a>
</content>
</entry>
<entry>
<id>http://www.cnblogs.com/change4now/p/5690450.html</id>
<title type="text">C# .net 使用 SmtpClient 发邮件 ,发送邮箱的配置 - change_4_now</title>
<summary type="text">
1.需打开POP3/SMTP/IMAP 2.打开时要求授权码,输入自定义的密码如:1234cb 3.自定义的密码就是 SmtpClient 的密码,而非邮箱密码
</summary>
<published>2016-07-21T00:58:00Z</published>
<updated>2016-07-21T00:58:00Z</updated>
<author>
<name>change_4_now</name>
<uri>http://www.cnblogs.com/change4now/</uri>
</author>
<link rel="alternate" href="http://www.cnblogs.com/change4now/p/5690450.html"/>
<link rel="alternate" type="text/html" href="http://www.cnblogs.com/change4now/p/5690450.html"/>
<content type="html">
【摘要】1.需打开POP3/SMTP/IMAP 2.打开时要求授权码,输入自定义的密码如:1234cb 3.自定义的密码就是 SmtpClient 的密码,而非邮箱密码 <a href="http://www.cnblogs.com/change4now/p/5690450.html" target="_blank">阅读全文</a>
</content>
</entry>
<entry>
<id>http://www.cnblogs.com/change4now/p/5665227.html</id>
<title type="text">关于onClick 提交数据问题 - change_4_now</title>
<summary type="text">
我在添加 民工考勤表,用ajax 自动读取数据添加到相应模块之后 进行 OnClick="btnSubmit_Click" 保存,结果无法保存,之后我将光标锁定到某一个文本框内,就可以保存了
</summary>
<published>2016-07-12T14:49:00Z</published>
<updated>2016-07-12T14:49:00Z</updated>
<author>
<name>change_4_now</name>
<uri>http://www.cnblogs.com/change4now/</uri>
</author>
<link rel="alternate" href="http://www.cnblogs.com/change4now/p/5665227.html"/>
<link rel="alternate" type="text/html" href="http://www.cnblogs.com/change4now/p/5665227.html"/>
<content type="html">
【摘要】我在添加 民工考勤表,用ajax 自动读取数据添加到相应模块之后 进行 OnClick="btnSubmit_Click" 保存,结果无法保存,之后我将光标锁定到某一个文本框内,就可以保存了 <a href="http://www.cnblogs.com/change4now/p/5665227.html" target="_blank">阅读全文</a>
</content>
</entry>
<entry>
<id>http://www.cnblogs.com/change4now/p/5665230.html</id>
<title type="text">http请求数据 - change_4_now</title>
<summary type="text">
/// <summary> /// http请求post数据 /// </summary> /// <param name="postData">数据</param> /// <param name="url">url</param> private void httppostdata(string
</summary>
<published>2016-07-12T14:49:00Z</published>
<updated>2016-07-12T14:49:00Z</updated>
<author>
<name>change_4_now</name>
<uri>http://www.cnblogs.com/change4now/</uri>
</author>
<link rel="alternate" href="http://www.cnblogs.com/change4now/p/5665230.html"/>
<link rel="alternate" type="text/html" href="http://www.cnblogs.com/change4now/p/5665230.html"/>
<content type="html">
【摘要】/// <summary> /// http请求post数据 /// </summary> /// <param name="postData">数据</param> /// <param name="url">url</param> private void httppostdata(string <a href="http://www.cnblogs.com/change4now/p/5665230.html" target="_blank">阅读全文</a>
</content>
</entry>
<entry>
<id>http://www.cnblogs.com/change4now/p/5665222.html</id>
<title type="text">一般处理文件 - change_4_now</title>
<summary type="text">
: Page, IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { var action = context.Request["action"]; switch (action
</summary>
<published>2016-07-12T14:48:00Z</published>
<updated>2016-07-12T14:48:00Z</updated>
<author>
<name>change_4_now</name>
<uri>http://www.cnblogs.com/change4now/</uri>
</author>
<link rel="alternate" href="http://www.cnblogs.com/change4now/p/5665222.html"/>
<link rel="alternate" type="text/html" href="http://www.cnblogs.com/change4now/p/5665222.html"/>
<content type="html">
【摘要】: Page, IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { var action = context.Request["action"]; switch (action <a href="http://www.cnblogs.com/change4now/p/5665222.html" target="_blank">阅读全文</a>
</content>
</entry>
<entry>
<id>http://www.cnblogs.com/change4now/p/5665224.html</id>
<title type="text">C#解析Json字符串 - change_4_now</title>
<summary type="text">
1.var resultStr={"from":"en","to":"zh","trans_result":[{"src":"apple","dst":"u82f9u679c"}]} var data = JObject.Parse(resultStr); if (data.Property("
</summary>
<published>2016-07-12T14:48:00Z</published>
<updated>2016-07-12T14:48:00Z</updated>
<author>
<name>change_4_now</name>
<uri>http://www.cnblogs.com/change4now/</uri>
</author>
<link rel="alternate" href="http://www.cnblogs.com/change4now/p/5665224.html"/>
<link rel="alternate" type="text/html" href="http://www.cnblogs.com/change4now/p/5665224.html"/>
<content type="html">
【摘要】1.var resultStr={"from":"en","to":"zh","trans_result":[{"src":"apple","dst":"u82f9u679c"}]} var data = JObject.Parse(resultStr); if (data.Property(" <a href="http://www.cnblogs.com/change4now/p/5665224.html" target="_blank">阅读全文</a>
</content>
</entry>
<entry>
<id>http://www.cnblogs.com/change4now/p/5665217.html</id>
<title type="text">后台提示 - change_4_now</title>
<summary type="text">
/// <summary> /// 提示 民工考勤提示 /// </summary> /// <param name="msg">提示内容</param> /// <param name="url">跳转url</param> /// <param name="timeOut">延迟</param>
</summary>
<published>2016-07-12T14:47:00Z</published>
<updated>2016-07-12T14:47:00Z</updated>
<author>
<name>change_4_now</name>
<uri>http://www.cnblogs.com/change4now/</uri>
</author>
<link rel="alternate" href="http://www.cnblogs.com/change4now/p/5665217.html"/>
<link rel="alternate" type="text/html" href="http://www.cnblogs.com/change4now/p/5665217.html"/>
<content type="html">
【摘要】/// <summary> /// 提示 民工考勤提示 /// </summary> /// <param name="msg">提示内容</param> /// <param name="url">跳转url</param> /// <param name="timeOut">延迟</param> <a href="http://www.cnblogs.com/change4now/p/5665217.html" target="_blank">阅读全文</a>
</content>
</entry>
<entry>
<id>http://www.cnblogs.com/change4now/p/5665221.html</id>
<title type="text">xml格式化成json - change_4_now</title>
<summary type="text">
JsonConvert.SerializeObject(model) XmlDocument doc = new XmlDocument(); doc.LoadXml(cam.Cam_Config); var camConfig = Newtonsoft.Json.JsonConvert.Seria
</summary>
<published>2016-07-12T14:47:00Z</published>
<updated>2016-07-12T14:47:00Z</updated>
<author>
<name>change_4_now</name>
<uri>http://www.cnblogs.com/change4now/</uri>
</author>
<link rel="alternate" href="http://www.cnblogs.com/change4now/p/5665221.html"/>
<link rel="alternate" type="text/html" href="http://www.cnblogs.com/change4now/p/5665221.html"/>
<content type="html">
【摘要】JsonConvert.SerializeObject(model) XmlDocument doc = new XmlDocument(); doc.LoadXml(cam.Cam_Config); var camConfig = Newtonsoft.Json.JsonConvert.Seria <a href="http://www.cnblogs.com/change4now/p/5665221.html" target="_blank">阅读全文</a>
</content>
</entry>
<entry>
<id>http://www.cnblogs.com/change4now/p/5665213.html</id>
<title type="text">手机端Post 数据 - change_4_now</title>
<summary type="text">
var data =""; using (StreamReader readStream = new StreamReader(context.Request.InputStream, Encoding.UTF8)) { data = readStream.ReadToEnd(); } var pa
</summary>
<published>2016-07-12T14:46:00Z</published>
<updated>2016-07-12T14:46:00Z</updated>
<author>
<name>change_4_now</name>
<uri>http://www.cnblogs.com/change4now/</uri>
</author>
<link rel="alternate" href="http://www.cnblogs.com/change4now/p/5665213.html"/>
<link rel="alternate" type="text/html" href="http://www.cnblogs.com/change4now/p/5665213.html"/>
<content type="html">
【摘要】var data =""; using (StreamReader readStream = new StreamReader(context.Request.InputStream, Encoding.UTF8)) { data = readStream.ReadToEnd(); } var pa <a href="http://www.cnblogs.com/change4now/p/5665213.html" target="_blank">阅读全文</a>
</content>
</entry>
</feed>

  

from xml.etree import ElementTree
root = ElementTree.XML(open('baoan.xml','r',encoding='utf-8').read())#打开本地的xml文件,传进XML模块
print(root.tag)#拿到顶层的tag

  执行结果如下图,这里拿到的是顶层标签,然后可以循坏里面的标签了

下面是一顿更加详细用法的代码,代码接着上面的

for i in root:#循坏顶层标签里面的所有下一层的标签
    try:
        #print(type(i))
        print(i.tag,i.attrib,i.find('name'))#查找子节点i下面的name节点
    except:
        print('i下面没有这个子节点')

  效果如图,这里必须说明一下,这里把xml语言里面的顶层标签的第一句<feed xmlns=”http://www.w3.org/2005/Atom”>里面xmlns=”http://www.w3.org/2005/Atom”语句去掉了,不然是无法查找子节点的

 还可以根据具体的标签符号查找某个类型的标签,如图,这里表示迭代地寻找

 

还有通过parse方法打开xml文件,这样可以实现对文件的修改

from xml.etree import ElementTree



tree = ElementTree.parse('banan.xml')#通过parse方式打开xml文件,这样就可读可写了

root = tree.getroot()#获取文件的root

for node in root.iter('entry'):
    new_text = 'new_str'
    node.text=str(new_text)#给entry标签加入文字内容,之前是没有的
    node.set('name','kai')#给entey设置属性
    node.set('age', '33')#给entey设置第二个属性
    print(node.attrib,node.text)
    del node.attrib['name']#删除entryname属性
    print(node.attrib, node.text)
tree.write('banan.xml')#修改之后要写回去覆盖之前的文件,这样修改才能起效,因为文件的打开修改操作是在内存里面,是没有保存的

  效果如图所示

 我们还可以自己生成xml语言,如图代码

from xml.etree import ElementTree

xml_test= ElementTree.Element('job_list')#创建根节点

name1 =ElementTree.SubElement(xml_test,'engineer',attrib={'gender':'woman'})#创建子节点

age1 =ElementTree.SubElement(xml_test,'age')#创建子节点
age1.text = '33'#创建子节点属性

et= ElementTree.ElementTree(xml_test)#生成文档对象
et.write('test.xml',encoding='utf-8',xml_declaration=True)#生成文件

  效果如图

 

《Python常用模块之xml模块》有一个想法
  1. Wow, incredible weblog structure! How lengthy have you been running a blog for?
    you made running a blog look easy. The overall look of your web site is magnificent, let alone the content material!

    You can see similar here sklep internetowy

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注