例子

import xml.dom.minidom

# 在内存中创建一个空的文档
doc = xml.dom.minidom.Document()
# 创建一个根节点Managers对象
root = doc.createElement('Managers')
# 设置根节点的属性
root.setAttribute('company', 'xx科技')
root.setAttribute('address', '科技软件园')
# 将根节点添加到文档对象中
doc.appendChild(root) managerList = [{'name':'joy', 'age':24, 'sex':'女'},
{'name':'tom', 'age':20, 'sex':'男'},
{'name':'ruby', 'age':30, 'sex':'女'}
] for i in managerList:
nodeManager = doc.createElement('Manager')
nodeName = doc.createElement('name')
# 给叶子节点name设置一个文本节点,用于显示玩文本内容
nodeName.appendChild(doc.createTextNode(str(i['name']))) nodeAge = doc.createElement('Age')
nodeAge.appendChild(doc.createTextNode(str(i['age']))) nodeSex = doc.createElement('sex')
nodeSex.appendChild(doc.createTextNode(str(i['sex']))) # 将各子叶节点添加到父节点Manager中,
# 最后将Manager添加到根节点Managers中
nodeManager.appendChild(nodeName)
nodeManager.appendChild(nodeAge)
nodeManager.appendChild(nodeSex)
root.appendChild(nodeManager) # 开始写xml文件
fp = open('Manager.xml', 'w')
doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding='utf-8')

实战获取双色球开奖数据,并 保存为xml格式

# -*- coding: UTF-8 -*-
import requests
import parsel
import xml.dom.minidom
import datetime #base_url='http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart=2003-01-01&dayEnd=2021-02-19&pageNo=14'
base_url='https://kaijiang.78500.cn/ssq/' header={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36',
'referer': 'https://kaijiang.78500.cn/ssq/'
} formdata={
'startqi':'',
'endqi':'',
'year':'',
'action': 'years' } # 在内存中创建一个空的文档
doc = xml.dom.minidom.Document()
# 创建一个根节点shuanseqiu对象
root = doc.createElement('shuanseqiu')
# 设置根节点的属性
root.setAttribute('name', '双色球')
root.setAttribute('time', str(datetime.datetime.now().strftime('%Y-%m-%d')))
# 将根节点添加到文档对象中
doc.appendChild(root) #打开文件
fp = open(r'F:\python_project\爬虫\双色球\shuangseqiu.xml', 'a+') for year in range(2003,2022):
formdata['year']=str(year)
formdata['startqi']=str(year)+'001'
formdata['endqi']=str(year)+'154'
respone=requests.post(base_url,headers=header,data=formdata)
print('==============正在获取%s年彩票数据===============',%year) html_data= parsel.Selector(respone.text) trs= html_data.xpath('//table[@class="kj-list-tab"]/tbody[2]/tr') for item in trs:
if item.xpath('./td[3]/div'):
expect=item.xpath('./td[1]/text()').extract_first()
opentime=item.xpath('./td[2]/text()').extract_first()
opencode=item.xpath('./td[3]/div').xpath('string(.)').extract_first().replace('\n',' ') #print(expect,opencode,opentime) nodeRow = doc.createElement('Row')
node_expect = doc.createElement('expect')
# 给叶子节点name设置一个文本节点,用于显示玩文本内容
node_expect.appendChild(doc.createTextNode(expect))
node_opencode = doc.createElement('opencode')
node_opencode.appendChild(doc.createTextNode(opencode))
node_opentime = doc.createElement('opentime')
node_opentime.appendChild(doc.createTextNode(opentime)) # 将各子叶节点添加到父节点Row中,
# 最后将Row添加到根节点shuangseqiu中
nodeRow.appendChild(node_expect)
nodeRow.appendChild(node_opencode)
nodeRow.appendChild(node_opentime)
root.appendChild(nodeRow) #写入文件
doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding='utf-8') fp.close()

最新文章

  1. JavaScript高级程序设计学习笔记--高级技巧
  2. ubuntu启动tomcat出错解决方案
  3. c# 远程连接ORACLE数据库
  4. 开创学习的四核时代-迅为iTOP4412学习开发板
  5. 配置错误定义了重复的“system.web.extensions/scripting/scriptResourceHandler” 解决办法
  6. mysql多实例部署
  7. select into from和insert into select from两种表复制语句区别
  8. linq里面似in的查询
  9. find()与children()方法的区别
  10. linux新学篇
  11. Django学习日记04_模板_overview
  12. C#编写街道管理系统
  13. bzoj4904 [Ctsc2017]最长上升子序列
  14. Python——Set集合
  15. 【代码笔记】Web-HTML-颜色
  16. PYTHON-网络通信 TCP
  17. PHP给图片加水印
  18. 【WP8】ResourceDictionary
  19. [转]Linux内核源码详解--iostat
  20. 我的Java学习笔记-语法

热门文章

  1. GPT接入飞书
  2. cowtransfer(奶牛快传)自动上传文件脚本—流程分析
  3. cximage函数总结(拷贝)
  4. 第六周作业-N67044-张铭扬
  5. JAVA操作Mongo 数组模糊查询
  6. WDA学习(29):WDA & HTML
  7. Mac新手必看Mac入门基本知识图文教程
  8. appium:报错Message: Message: Parameters were incorrect. We wanted {"required":["value"]}
  9. 题解[LuoguP6222]「P6156简单题」加强版
  10. 01爬取豆瓣网电影数据进行numpy的练习