python 分别使用Python中自带的xml.etree.ElementTree和第三方LXML来生成一个按首字母归类的城市列表XML文件。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 分别使用Python中自带的xml.etree.ElementTree和第三方LXML来生成一个按首字母归类的城市列表XML文件。相关的知识,希望对你有一定的参考价值。
#! /usr/bin/env python
# coding=utf-8
__author__ = 'Kingson.Zhou'
import requests
import string
from pyquery import PyQuery as pq
import xml.etree.ElementTree as ET
from lxml import etree as ET
# Get city list from MeiTuan Website
def create_city():
city_request = requests.get("http://www.meituan.com/index/changecity/initiative")
city_content = city_request.content
p = pq(city_content)
city = []
for i in string.uppercase:
filter = '#city-' + i
city_list = p('li').filter(filter).text()
if city_list != None:
city.append(city_list.split())
return city
# Create a xml file by xml.etree.ElementTree
def building_xml():
city = create_city()
root = ET.Element("plist")
root.set("version", "1.0")
array = ET.SubElement(root, "array")
for i in range(len(city)):
dict = ET.SubElement(array, "dict")
key = ET.SubElement(dict, "key")
key.text = "Title"
string = ET.SubElement(dict, "string")
string.text = city[i][0]
key = ET.SubElement(dict, "key")
key.text = "Cities"
array1 = ET.SubElement(dict, "array")
for n in range(1,len(city[i])):
dict = ET.SubElement(array1, "dict")
key = ET.SubElement(dict, "key")
key.text = "Title"
string = ET.SubElement(dict, "string")
string.text = city[i][n]
tree = ET.ElementTree(root)
tree.write("cities.xml", encoding="UTF-8")
return True
# Pretty a xml file
def pretty_xmlfile():
parser = ET.XMLParser(remove_blank_text=False, resolve_entities=True, strip_cdata=True)
xmlfile = ET.parse("cities.xml",parser)
pretty_xml = ET.tostring(xmlfile, encoding = 'UTF-8', xml_declaration = True, pretty_print = True,doctype='<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">')
file = open("cities.xml","w")
file.writelines(pretty_xml)
file.close()
# Create a xml file by lxml
def create_xml():
city = create_city()
plist = ET.Element('plist')
plist.attrib['version']= '1.0'
array = ET.SubElement(plist, 'array')
for i in range(len(city)):
dict = ET.SubElement(array, 'dict')
key = ET.SubElement(dict, 'key')
key.text = "Title"
string = ET.SubElement(dict, "string")
string.text = city[i][0]
key = ET.SubElement(dict, "key")
key.text = "Cities"
array1 = ET.SubElement(dict, 'array')
for n in range(1,len(city[i])):
dict = ET.SubElement(array1, "dict")
key = ET.SubElement(dict, "key")
key.text = "Title"
string = ET.SubElement(dict, "string")
string.text = city[i][n]
print ET.tostring(plist, pretty_print=True, xml_declaration=True, encoding='UTF-8', doctype='<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">')
# write to file:
tree = ET.ElementTree(plist)
tree.write('cities.xml', pretty_print=True, xml_declaration=True, encoding='UTF-8')
return True
if __name__ == '__main__':
building_xml()
# pretty_xmlfile()
# create_xml()
以上是关于python 分别使用Python中自带的xml.etree.ElementTree和第三方LXML来生成一个按首字母归类的城市列表XML文件。的主要内容,如果未能解决你的问题,请参考以下文章
[python小记]使用lxml修改xml文件,并遍历目录
如何使用python生成xml
python导入自带模块和自定义模块
python怎么解析xml文件
python3 解析xml
python3.3不自带的模块或工具包 下载以后要怎么处理才能在开发环境中调用 (比如放到pyth