Python语法学习
import urllib
import sys
import xml.etree.ElementTree as etree
def get_url_list(apiurl):
xml_data = urllib.urlopen(apiurl).read()
city_xml_data = etree.fromstring(xml_data)
return list(city_xml_data.find("divisions").iter("division"))
def get_shop_list(apiurl):
xmldata = urllib.urlopen(apiurl).read()
xml_data = etree.fromstring(xmldata)
return list(xml_data.iter("shop"))
#构造城市地址列表
city_ids = list()
links = get_url_list('http://www.meituan.com/api/v1/divisions')
for division in links:
city_ids.append("http://www.nuomi.com/api/dailydeal?version=v1&city="+division.find("id").text)
#print division.find("name").text.encode("utf-8")
for city_id in city_ids:
shops_xml_list = get_shop_list(city_id)
for shops_xml in shops_xml_list:
if (not shops_xml.find("name").text is None) and (not shops_xml.find("name") == "" ):
print shops_xml.find("name").text.encode("utf-8")
#if (not shops_xml.find("addr").text is None) and (not shops_xml.find("addr") == "" ):
#print shops_xml.find("addr").text.encode("utf-8")
页:
[1]