python第三方模块xmltodict库优雅处理xml格式为json
作者:菜鸟童学
今天给大家带来一篇关于python第三方模块xmltodict的使用文章。
1、背景:
工作中我们经常处理一些xml格式的数据,比如在抓取某网站数据时,我们会取巧去抓取他的RSS订阅信息等,那我们怎么解析这些数据呢,有一些传统的类似html lxml的工具包,今天给大家带来一个xmltodict 优雅处理xml格式为json的库
2、什么是 XML?
XML 指可扩展标记语言(EXtensible Markup Language)。
XML 是一种很像HTML的标记语言。
XML 的设计宗旨是传输数据,而不是显示数据。
XML 标签没有被预定义。您需要自行定义标签。
XML 被设计为具有自我描述性。
XML 是 W3C 的推荐标准。
3、什么是 RSS?
RSS 指 Really Simple Syndication(真正简易联合)
RSS 使您有能力聚合(syndicate)网站的内容
RSS 定义了非常简单的方法来共享和查看标题和内容
RSS 文件可被自动更新
RSS 允许为不同的网站进行视图的个性化
RSS 使用 XML 编写
4、xmltodict安装
pip3 install xmltodict
5、我们看下如何使用
import xmltodict
import json
xml_str = '''
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
'''
new_dict_obj = xmltodict.parse(xml_str) # 返回一个OrderedDict类型的对象
json_str = json.dumps(new_dict_obj) # 使用内置的json模块转换成json
print(json_str)结果如下:
{"bookstore": {"book": [{"@category": "COOKING", "title": {"@lang": "en", "#text": "Everyday Italian"}, "author": "Giada De Laurentiis", "year": "2005", "price": "30.00"}, {"@category": "CHILDREN", "title": {"@lang": "en", "#text": "Harry Potter"}, "author": "J K. Rowling", "year": "2005", "price": "29.99"}, {"@category": "WEB", "title": {"@lang": "en", "#text": "Learning XML"}, "author": "Erik T. Ray", "year": "2003", "price": "39.95"}]}}
如何转成xml
直接上代码
import xmltodict
new_dict_obj = {"bookstore": {"book": [{"@category": "COOKING", "title": {"@lang": "en", "#text": "Everyday Italian"}, "author": "Giada De Laurentiis", "year": "2005", "price": "30.00"}, {"@category": "CHILDREN", "title": {"@lang": "en", "#text": "Harry Potter"}, "author": "J K. Rowling", "year": "2005", "price": "29.99"}, {"@category": "WEB", "title": {"@lang": "en", "#text": "Learning XML"}, "author": "Erik T. Ray", "year": "2003", "price": "39.95"}]}}
new_xml = xmltodict.unparse(new_dict_obj)# 这里直接放dict对象
print(new_xml)结果如下
<?xml version="1.0" encoding="utf-8"?>
<bookstore><book category="COOKING"><title lang="en">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book><book category="CHILDREN"><title lang="en">Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book><book category="WEB"><title lang="en">Learning XML</title><author>Erik T. Ray</author><year>2003</year><price>39.95</price></book></bookstore>
6、网站抓取中使用
获取xml数据源链接

请求数据源
import requests
import xmltodict
headers = {
}
url = "https://boyyongxin.github.io/atom.xml"
response = requests.get(url, headers=headers,verify=False)
xml_str = response.text
new_dict_obj = xmltodict.parse(xml_str)
print(new_dict_obj)得到的结果我们就可以json方式去提取了
以上就是python第三方模块xmltodict库优雅处理xml格式为json的详细内容,更多关于python xmltodict库的资料请关注脚本之家其它相关文章!
