importrequestsimportxlwt#返回一个字典,键是各个省份的名字,值是对应省份的网址url
defgetProvinceCode(url):
response=requests.get(url)
response.encoding=response.apparent_encoding
content=response.text
start= content.find('') + len('') + len("\n")
end= content.find('')
mapStr=content[start:end]#print(mapStr)
lines = mapStr.split("\n")
baseUrl= 'http://www.ip138.com/'city_urls=[]
city_name=[]for line inlines:ifline:
index1= line.find('href="/') + len('href="/')
index2= line.find('/"')
code=line[index1:index2]
url= baseUrl +code
city_urls.append(url)
title1= line.find('title="')+len('title="')
title2= line.find('"', title1)
title=line[title1:title2]
city_name.append(title)
dict_prov_url=dict(zip(city_name,city_urls))for item in dict_prov_url.items(): #显示各个省份名称和对应的url
print(item)returndict_prov_url#根据url得到省份的各个城市的城市名、邮政编码以及长途区号,返回一个二维的列表。
defgetPostCode(url):
response=requests.get(url)
response.encoding=response.apparent_encoding
content=response.text
start= content.find('长途区号') + len("长途区号")
end= content.find('', start)
add_post=content[start:end]
posts= add_post.strip().split('
') #posts为每一个去掉组成的列表code_list =[]for post inposts:ifpost:
lines= post.strip().split('
= 2:if 'nbsp' in lines[4]:if len(lines) >= 6:if 'nbsp' in lines[5]:test=[]
city= lines[1][lines[1].find('>')+len('>'):lines[1].find('')]
post_code= lines[2][lines[2].find('">')+len('">'):lines[2].find('')]
area_code= lines[3][lines[3].find('">')+len('">'):lines[3].find('')]
test.append(city)
test.append(post_code)
test.append(area_code)
code_list.append(test)else:
test=[]
city= lines[1][lines[1].find('')+len(''):lines[1].find('')]
post_code= lines[2][lines[2].find('">')+len('">'):lines[2].find('')]
area_code= lines[3][lines[3].find('">')+len('">'):lines[3].find('')]
test.append(city)
test.append(post_code)
test.append(area_code)
code_list.append(test)else:
test1=[]
city= lines[1][lines[1].find('>')+len('>'):lines[1].find('')]
post_code= lines[2][lines[2].find('">')+len('">'):lines[2].find('')]
area_code= lines[3][lines[3].find('">')+len('">'):lines[3].find('')]
test1.append(city)
test1.append(post_code)
test1.append(area_code)
code_list.append(test1)
test2=[]
city= lines[4][lines[4].find('>')+len('>'):lines[4].find('')]
post_code= lines[5][lines[5].find('">')+len('">'):lines[5].find('')]
area_code= lines[6][lines[6].find('">')+len('">'):lines[6].find('')]
test2.append(city)
test2.append(post_code)
test2.append(area_code)
code_list.append(test2)
showPost(code_list)returncode_list#在终端上显示上面getPostCode(url)函数的得到二维的列表
defshowPost(code_list):for i inrange(len(code_list)):print(code_list[i])#写入excel文件
defwrite_excel(path):#创建工作簿
workbook = xlwt.Workbook(encoding='utf-8')#创建sheet
for title,url in getProvinceCode('http://www.ip138.com/post/').items():
data_sheet=workbook.add_sheet(title)
row0= [u'城市名称', u'邮政编码', u'长途区号'] #每个表的第一行文字,表头
for i inrange(len(row0)):
data_sheet.write(0, i, row0[i])
code_list=getPostCode(url)for i in range(len(code_list)): #循环写入所有邮政编码信息
for j inrange(len(code_list[i])):
data_sheet.write(i+1,j,code_list[i][j])
workbook.save(path)if __name__ == '__main__':
path= './postcode.xls'write_excel(path)print(u'写入postcode.xls文件成功')