真正完全独立没参考任何代码没进行任何搜索的第一个爬虫,网站无robots.txt:

import requests
import time
import re
from bs4 import BeautifulSoup
from random import random# get the whole information of html page from the index page.
def get_html_text(url, code='utf-8'):kv = {'User-Agent': 'Mozilla/5.0'}try:res = requests.get(url, timeout=60, headers=kv)res.raise_for_status()res.encoding = codereturn res.textexcept BaseException:return ''# get the excel file and store from the detailed page.
def get_excel_store(url, filepath='d:/'):try:time.sleep(random())r = get_html_text(url)soup = BeautifulSoup(r, 'html.parser')# span is a special tag in this resource page with a style5 propertyspan = soup.find_all('span', attrs={'class': 'STYLE5'})[0]# find <a> in span and get information at the same time.for a in span.children:file_path = filepath + a.textxls = requests.get(a.attrs.get('href'), timeout=30)xls.raise_for_status()with open(file_path, 'wb') as newfile:newfile.write(xls.content)except BaseException:return ''def get_all_html(url, urllist2):r = get_html_text(url)soup = BeautifulSoup(r, 'html.parser')a = soup.find_all('a', attrs={'target': '_blank'})re_text = r'[/][\d]{6}[/][t][\d]{8}[_][\d]{2,8}'for i in a:try:href = i.attrs['href']urls = re.findall(re_text, href)for url in urls:if url == '':continueelse:urllist2.append('http://www.stats.yn.gov.cn/tjsj/jdsj' + url + '.html')except BaseException:continuedef main():filepath = 'D:/'url = 'http://www.stats.yn.gov.cn/tjsj/jdsj/index'# 构造翻页url列表urllist = []depth = 2for i in range(depth):if i == 0:urllist.append(url + '.html')else:urllist.append(url + '_' + str(i) + '.html')# 对每一页面中url提取信息:for url in urllist:r = get_html_text(url)urllist2 = []# 获取当前页面中所有的下级url,并存储在列表中get_all_html(url, urllist2)# 对每个下级url进行获取xls操作for url1 in urllist2:get_excel_store(url1)main()

编程花了得1小时,还是太慢,结果见下图

Python爬取云南统计局数据报表相关推荐

  1. Python应用实战-Python爬取4000+股票数据,并用plotly绘制了树状热力图(treemap)

    目录: 1. 准备工作 2. 开始绘图 2.1. 简单的例子 2.2. px.treemap常用参数介绍 2.3. color_continuous_scale参数介绍 2.4. 大A股市树状热力图来 ...

  2. python爬去朋友圈_利用Python爬取朋友圈数据,爬到你开始怀疑人生

    人生最难的事是自我认知,用Python爬取朋友圈数据,让我们重新审视自己,审视我们周围的圈子. 文:朱元禄(@数据分析-jacky) 哲学的两大问题:1.我是谁?2.我们从哪里来? 本文 jacky试 ...

  3. python 爬取拉钩数据

    Python通过Request库爬取拉钩数据 爬取方法 数据页面 建表存储职位信息 解析页面核心代码 完整代码 结果展示 爬取方法 采用python爬取拉钩数据,有很多方法可以爬取,我采用的是通过Re ...

  4. python 爬取拉钩网数据

    python 爬取拉钩网数据 完整代码下载:https://github.com/tanjunchen/SpiderProject/blob/master/lagou/LaGouSpider.py # ...

  5. 利用Python爬取国家水稻数据中心的品种数据

    利用Python爬取国家水稻数据中心的品种数据 一.页面获取 python可以进行对网页的访问,主要用到requests,beautifulsoup4包. 首先新建一个page的py文件,用来获取页面 ...

  6. 利用Python爬取朋友圈数据,爬到你开始怀疑人生

    人生最难的事是自我认知,用Python爬取朋友圈数据,让我们重新审视自己,审视我们周围的圈子. 文:朱元禄(@数据分析-jacky) 哲学的两大问题:1.我是谁?2.我们从哪里来? 本文 jacky试 ...

  7. 利用python爬取2019-nCoV确诊数据并制作pyecharts可视化地图

    1.本章利用python爬取2019-nCoV确诊数据并制作pyecharts可视化地图: 2.主要内容为绘制出中国各省疫情数据,疫情数据从四个维度进行可视化展示:累积确诊人数.现存确诊人数.治愈人数 ...

  8. 用Python爬取最新股票数据含完整源代码

    用Python爬取最新股票数据含完整源代码 抓取目标: url:http://webapi.cninfo.com.cn/#/marketDataDate 数据目标: 获取 证券代码 证券简称 交易日期 ...

  9. 使用python爬取喜马拉雅音频数据并保存

    ** 使用python爬取喜马拉雅音频数据并保存 ** 1.进入喜马拉雅官网,打开要爬取的项目网页,按F12=>F5后进行清空,点击项目网页中播放按钮,出现如下图点击,查找网页的url,获取到网 ...

最新文章

  1. hdu 1251 统计难题
  2. 【DIY】玩转VFD荧光屏,自制VFD时钟全资料(原理图+源码+PCB)
  3. python 多进程multiprocessing进程池pool tensorflow-yolov3 报错 MemoryError
  4. $lookup做关联表查询
  5. 帝国cms重置管理员_重新设计《纽约时报》 CMS飞行员
  6. Android4.4的init进程
  7. 第一篇博客 记录自己对spring bean的理解
  8. 使用javascript实现在页面打印的效果的三种方式
  9. 解决(Missing artifact com.oracle:ojdbc14:jar:11.2.0.4.0)
  10. Atitit springboot mybatis spring 集成 Springboot1.4 mybatis3.4.6 /springbootMybatis 目录 1.1. 设置map
  11. get() got an unexpected keyword argument
  12. 当不知轴承型号时如何寻找轴承故障频率_轴承故障的检测,处理
  13. android(微博 微信 qq) 分享和第三分认证登录的封装
  14. Doevent()理解
  15. 计蒜客 - A1503
  16. 【Python】多线程爬取某站高颜值小姐姐照片(共1.62GB)
  17. 前端词汇(后续整理)
  18. 【Java】实现计算器
  19. thonny中文版|python编辑
  20. 关于 getWriter() has already been called for this response 的错误解决办法

热门文章

  1. JavaWeb和WebGIS学习笔记(三)——GeoServer 发布shp数据地图
  2. 【数据库和SQL学习笔记】1.SQL语言的功能和特点,数据定义语言和应用,主键和外键
  3. 如何快速的获取微博内容?
  4. sos.exe病毒的手动查杀方法(AUTO病毒)
  5. 以太坊智能合约的生命周期
  6. Java实现各种视频格式下载
  7. SAP固定资产的几个相关日期
  8. (十二)python图形用户界面
  9. 百度文库复制不了文字!!!
  10. 论文翻译:ViBe+算法(ViBe算法的改进版本)