python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python xmltodict库

python第三方模块xmltodict库优雅处理xml格式为json

作者:菜鸟童学

这篇文章主要为大家介绍了python第三方模块xmltodict库优雅处理xml格式为json实例探究,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

今天给大家带来一篇关于python第三方模块xmltodict的使用文章。

1、背景:

工作中我们经常处理一些xml格式的数据,比如在抓取某网站数据时,我们会取巧去抓取他的RSS订阅信息等,那我们怎么解析这些数据呢,有一些传统的类似html  lxml的工具包,今天给大家带来一个xmltodict 优雅处理xml格式为json的库

2、什么是 XML?

3、什么是 RSS?

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库的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文