python存储xml格式数据
例子
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()