Skip to content

Instantly share code, notes, and snippets.

@Kingson
Created August 19, 2013 12:03
Show Gist options
  • Save Kingson/6268401 to your computer and use it in GitHub Desktop.
Save Kingson/6268401 to your computer and use it in GitHub Desktop.
分别使用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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment