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文件成功')
python输入城市找省份_python爬虫学习之爬取全国各省市县级城市邮政编码相关推荐
- python爬虫学习之爬取全国各省市县级城市邮政编码
实例需求:运用python语言在ip查询 查ip 网站ip查询 同ip网站查询 iP反查域名 iP查域名 同ip域名网站爬取全国各个省市县级城市的邮政编码,并且保存在excel文件中 实例环境:pyt ...
- python手机壁纸超清_python爬虫学习之爬取5K分辨率超清唯美壁纸
前言 Python现在非常火,语法简单而且功能强大,很多同学都想学Python!所以小的给各位看官们准备了高价值Python学习视频教程及相关电子版书籍,欢迎前来领取! 简介 壁纸的选择其实很大程度上 ...
- python大作业爬虫_Python爬虫学习-爬取大规模数据(10w级)
编译环境:python v3.5.0, mac osx 10.11.4 python爬虫基础知识: Python爬虫学习-基础爬取 了解数据库 MongoDB 数据库是储存数据的地方,可以将如下的字典 ...
- python爬取豆瓣书籍_python爬虫学习,爬取豆瓣各分类书单
点击蓝字"python教程"关注我们哟! 代码展示:pachon2.5.py # -- coding: utf-8 -- import urllib import urllib2 ...
- python爬虫可以爬什么山好_Python爬虫示例:爬取 13 个旅游城市,看看大家放假了都爱去哪玩...
距上次假期端午结束已经47天过去了...依然还是没有假期,想出去好多地方玩啊.你们是不是也在期待着假期呀,不过夏天也热,下次的假期大概就是中秋国庆了,那会儿还算是比较凉爽.心血来潮,就想爬一下大家假期 ...
- python中美元人汇率_Python爬虫练习:爬取美元历史汇率
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 以上文章来源于小鹏友的数据分析之路,作者 小鹏友 网址:https://chl. ...
- Python爬虫学习笔记 -- 爬取糗事百科
Python爬虫学习笔记 -- 爬取糗事百科 代码存放地址: https://github.com/xyls2011/python/tree/master/qiushibaike 爬取网址:https ...
- Python爬虫学习之爬取淘宝搜索图片
Python爬虫学习之爬取淘宝搜索图片 准备工作 因为淘宝的反爬机制导致Scrapy不能使用,所以我这里是使用selenium来获取网页信息,并且通过lxml框架来提取信息. selenium.lxm ...
- Python爬虫实战之 爬取全国理工类大学数量+数据可视化
上次爬取高考分数线这部分收了个尾,今天咱们来全面爬取全国各省有多少所理工类大学,并简单实现一个数据可视化.话不多说,咱们开始吧. 第一步,拿到url地址 第二步,获取高校数据 第三步,地图可视化 第四 ...
最新文章
- 为什么不敢和别人竞争_内心很脆弱,不敢和人竞争。该怎么办?
- va_list va_start va_end的使用
- maven项目打包时生成dependency-reduced-pom.xml
- 【渝粤教育】国家开放大学2018年秋季 2632T城市轨道交通客运组织 参考试题
- VirtualBox无法启动虚拟机的解决办法
- 在termux中利用安卓的vulkan库加速NCNN
- SVG—最简单的SVG动画
- Win11右下角音量图标不见了怎么办
- 【K-近邻】K-NN 实战分析 Facebook V Results: Predicting Check Ins
- 我的世界 服务器文件ess,我的世界指令大全 ess指令用法介绍
- mtk android 多媒体架构,MTK6739核心板
- Equalize Them All
- 【HTML5】网页实用技巧3:将方形图片设置成圆形后,添加圆形虚线边框
- 【Java】设计Java程序,假设有50瓶饮料,喝完3个空瓶可以换一瓶饮料,依次类推,请问总共喝了多少瓶饮料?
- 一些http和tomcat知识补充
- 支付宝小程引入 SJS
- python绘制派大星_简笔画:如何使用Flash绘制派大星 看完你就知道了
- 《学一辈子光线追踪》 三 光散射
- 第十章---《实时语音处理实践指南》语音编/解码笔记
- 普通人如何架设一个简单的服务器攻略
热门文章
- elementUI+nodejs中间件multer+mysql实现图片上传功能
- .Lucky勒索病毒删除+.Lucky后缀文件恢复工具
- 路由表包含哪些信息?如何配置浮动路由?
- 【转】开源如何盈利?
- 安卓盘点机PDA的WIFI连接
- 新闻头条自媒体系统php,新闻头条-新闻自媒体-技术博客织梦模板(带手机版)...
- pandas一阶差分
- 【踩坑日记】IDEA2021启动项目报错“找不到符号”实则lombok不兼容
- DAZ3D STUDIO 中复杂背景道具的简化
- 服务器测试常用指令-分测试内容