使用的python3.6

民政网站,不同年份数据可能页面结构不一致,这点踩了很多坑,这也是代码越写越长的原因。

如果以后此段代码不可用,希望再仔细学习下 页面结构是否发生了变更。

 # -*- coding: utf-8 -*-
"""
Created on Wed Jul 10 14:40:41 2019 @author: Administrator
""" import pandas as pd
import requests
from bs4 import BeautifulSoup
import time url1 = 'http://www.mca.gov.cn/article/sj/xzqh//1980/'
headers = {'content-type': 'application/json',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'} # 1. 获取所有链接========================================================================
def f1(url1):
'2018-1980年中华人民共和国行政区划代码 的所有链接'
#requests发出请求,设置url,header参数
response = requests.get(url1, headers=headers, timeout=200, verify=False)
soup = BeautifulSoup(response.text,'lxml') #将网页源码返回为BeautifulSoup类型
_tmp1 = soup.select('td.arlisttd')
end_1 = []
for i in _tmp1:
_a = i.select('a')[0].get('href')
_b = i.select('a')[0].get('title')[:4]
end_1.append(['http://www.mca.gov.cn'+_a,_b])
return end_1 end_2=[]
for i in ['','?2','?3']:
end_2 = end_2+f1(url1+i) def f2(url1='http://www.mca.gov.cn/article/sj/xzqh/2019/'):
'2019年中华人民共和国行政区划代码'
response = requests.get(url1, headers=headers, timeout=200, verify=False)
soup = BeautifulSoup(response.text,'lxml')
_tmp1 = soup.select('td.arlisttd')
end_1 = []
for i in _tmp1:
_a = i.select('a')[0].get('href')
_b = i.select('a')[0].get('title')[:7]
end_1.append(['http://www.mca.gov.cn'+_a,_b])
return end_1 end_2 = end_2+f2() # 2. 获取数据========================================================================
def f3(url1='http://www.mca.gov.cn/article/sj/xzqh/1980/201903/20190300014989.shtml'):
#url1='http://www.mca.gov.cn/article/sj/xzqh/1980/201507/20150715854922.shtml'
#url1='http://www.mca.gov.cn/article/sj/xzqh/1980/201507/20150715854918.shtml'
#
response = requests.get(url1, headers=headers, timeout=200, verify=False)
soup = BeautifulSoup(response.text,'lxml')
_txt = soup.select('script')[4].get_text().strip().replace('window.location.href="','').strip('";')
if _txt[-4:]=='html':
print('script!')
url2 = _txt
else:
_tmp1 = soup.select('div.artext > div > p > a')
if len(_tmp1)==0:
_tmp1 = soup.select('div#zoom > a')
url2 = _tmp1[0].get('href')
print(url2)
#return url2
#url2='http://www.mca.gov.cn/article/sj/tjbz/a/201713/201708220856.html'
time.sleep(0.5)
response = requests.get(url2, headers=headers, timeout=200, verify=False)
#将网页源码返回为BeautifulSoup类型
soup = BeautifulSoup(response.text,'lxml')
_tmp1 = soup.select('table > tr[height="19"]')
end_1 = []
if len(_tmp1)>5:
for i in _tmp1:
_a = i.select('td')[1].get_text().strip()
if len(_a)>15: #部分数据页面,最后一行是备注。
continue
else:
_b = i.select('td')[2].get_text().strip()
end_1.append([_a,_b])
else:
_tmp1 = soup.select('table > tr[height="20"]')
for i in _tmp1:
_a = i.select('td')[0].get_text().strip()
if len(_a)>15 or _a=='行政区划代码': #部分数据页面,最后一行是备注。
continue
else:
_b = i.select('td')[1].get_text().strip()
end_1.append([_a,_b]) return end_1 #循环对每个链接 获取数据
end_3=[];#end_4=[]
for j in range(len(end_2)):
item = end_2[j]
if '' in item[1] or '' in item[1]:
print(j,item[0],item[1])
tmp2 = f3(item[0])
print('.')
end_3.extend([[item[1]]+i for i in tmp2])
#end_4.append(tmp2)
time.sleep(0.1) df_result = pd.DataFrame(end_3)
#pd.DataFrame(end_4).to_excel('所有连接.xlsx',index=False)
df_result.to_excel('地区编码.xlsx',index=False) '''
#\3 2019年5月份县以上行政区划代码_3852 > table > tbody > tr:nth-child(4)
#list_content > div.list_right > div > ul > table > tbody > tr:nth-child(1) > td.arlisttd > a
'''

最新文章

  1. jquery插件——点击交换元素位置(带动画效果)
  2. CSS学习总结(二)
  3. Mysql数据库表排序规则不一致导致联表查询,索引不起作用问题
  4. 用vs2010 编写C语言程序,VS2010 C++编译报错LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
  5. NGUI 3.5教程(六)Font字体
  6. Reverse Linked List 递归非递归实现
  7. Mongo服务器集群配置【转】
  8. BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏( dfs )
  9. Shell——数学计算
  10. 多态 OC——第十天
  11. CentOS6.x 下 /etc/security/limits.conf 被改错的故障经历
  12. 设置VS2017背景图片
  13. zabbix免客户端监控网站URL
  14. 009_python魔法函数
  15. 寻求js
  16. java中集合格式及json格式的特点和转换
  17. 【校招面试 之 C/C++】第10题 C++不在构造函数和析构函数中调用虚函数
  18. kubernetes 日志定制查阅 - 排错 -- 好用的命令
  19. spring注解@Value取不到值【转】
  20. bootstrap中的modal 模态弹出框不能放在 form_for里面,一弹出modal会自动submit掉form

热门文章

  1. selenium简单应用
  2. 2018-8-10-WPF-鼠标移动到列表上-显示列表图标
  3. EasyUI Tree与Datagrid联动
  4. Java实现数字大写转换
  5. 微信audio自动播放(ios播放问题)
  6. PAT甲级——A1020 Tree Traversals
  7. oracle创建新的连接(表空间?数据库?)
  8. C++11的for循环的新用法
  9. mysqldump mysql数据库导出命令
  10. Vue开发警告[Vue warn]: Avoid replacing instance root $data. Use nested data properties instead.