python输入城市找省份_python爬虫学习之爬取全国各省市县级城市邮政编码

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文件成功')

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值