python3学习(3):ID 遍历爬虫
从python3学习(2)中可知所有爬取的网站URL只有在结尾处有区别,因此,可以利用该弱点来遍历访问所有URL。
### 二、 ID 遍历爬虫,利用网站结构的弱点,轻松访问所有内容。 # Downloading: http://example.webscraping.com/places/default/view/Afghanistan-1 # Downloading: http://example.webscraping.com/places/default/view/Aland-Islands-2 # Downloading: http://example.webscraping.com/places/default/view/Albania-3 # Downloading: http://example.webscraping.com/places/default/view/Algeria-4 # Downloading: http://example.webscraping.com/places/default/view/American-Samoa-5 # Downloading: http://example.webscraping.com/places/default/view/Andorra-6 # Downloading: http://example.webscraping.com/places/default/view/Angola-7 ## 由上可知,这些 URL 只有结尾处有区别。 import urllib.request ## -- written by LiSongbo def Rocky_dnload(url,user_agent='wswp',num_retries = 2):print('Downloading:',url)LiSongbo_he={'User-agent':user_agent}request = urllib.request.Request(url, headers=LiSongbo_he)try: ## -- written by LiSongbo html = urllib.request.urlopen(request).read()except urllib.request.URLError as e: ## -- written by LiSongbo print('Download error:',e.reason)html = Noneif num_retries > 0: ## -- written by LiSongbo if hasattr(e,'code') and 500 <= e.code < 600:return Rocky_dnload(url,user_agent,num_retries-1) ## retry 5xx HTTP errors return htmlimport re ## -- written by LiSongbo def Rocky_crawl_sitemap(url): ## -- written by LiSongbo sitemap = Rocky_dnload(url) ## download the sitmap file sitemap = sitemap.decode('utf-8')links = re.findall('<loc>(.*?)</loc>', sitemap) ## extract the sitemap links from flag loc for link in links: ## download each link html = Rocky_dnload(link) ## crape html here import itertools ## -- written by LiSongbo max_errors = 5 n_errors = 0 for page in itertools.count(1): ## -- written by LiSongbo url = 'http://example.webscraping.com/view/-%d' % pagehtml = Rocky_dnload(url)if html is None: ## -- written by LiSongbo n_errors += 1if n_errors==max_errors:breakelse:n_errors = 0
运行结果如下:
Downloading: http://example.webscraping.com/view/-1
Downloading: http://example.webscraping.com/view/-2
Downloading: http://example.webscraping.com/view/-3
Downloading: http://example.webscraping.com/view/-4
Downloading: http://example.webscraping.com/view/-5
Downloading: http://example.webscraping.com/view/-6
Downloading: http://example.webscraping.com/view/-7
Downloading: http://example.webscraping.com/view/-8
Downloading: http://example.webscraping.com/view/-9
……
## -- written by LiSongbo
转载于:https://www.cnblogs.com/LiSongbo/p/9245585.html
python3学习(3):ID 遍历爬虫相关推荐
- python3学习(6):ID 遍历爬虫,将需要下载的网页数量最小化
从python3学习(5)中可知所有爬取的网站URL只有在结尾处有区别,因此,可以利用该弱点来遍历访问所有URL. ### 二. ID 遍历爬虫,利用网站结构的弱点,轻松访问所有内容. # Downl ...
- 用python写网络爬虫 -从零开始 3 编写ID遍历爬虫
我们在访问网站的时候,发现有些网页ID 是按顺序排列的数字,这个时候我们就可以使用ID遍历的方式来爬取内容.但是局限性在于有些ID数字在10位数左右,那么这样爬取效率就会很低很低! import it ...
- 爬虫入门(三)进阶技巧之ID遍历、追踪链接
1.使用id遍历 (1)原理 使用id遍历网页是常见的做法,由于大多数网站存储的数据太多,不可能为每一个网页都起名字,便用id做标记使得数据库方便识别,这也使得按id遍历网页成为可能. 在示例网站:h ...
- 2021-09-01 学习笔记:Python爬虫、数据可视化
2021-09-01 学习笔记:Python爬虫.数据可视化 结于2021-09-07: 内容来自 成都工业大学 数字媒体专业实训: 主要内容: PyCharm开发Python脚本的基础配置: Pyt ...
- Python学习教程:Python爬虫抓取技术的门道
Python学习教程:Python爬虫抓取技术的门道 web是一个开放的平台,这也奠定了web从90年代初诞生直至今日将近30年来蓬勃的发展.然而,正所谓成也萧何败也萧何,开放的特性.搜索引擎以及简单 ...
- Python3 学习系列 丨 博客目录索引
整个博客有关 Python 学习目录索引,方便快捷定位查询 基础学习篇 Python3 基础学习笔记 C01[变量和简单数据类型] Python3 基础学习笔记 C02[列表] Python3 基础学 ...
- Python3学习笔记之-学习基础(第三篇)
Python3学习笔记之-学习基础(第三篇) 文章目录 目录 Python3学习笔记之-学习基础(第三篇) 文章目录 一.循环 1.for循环 2.while循环 3.break,continue 二 ...
- Python3 学习笔记
Python3 学习笔记 1.基础语法 1.1 字符串操作 title() 将单词首字母改为大写 upper() 所有字母改为大写 lower() 所有字母改为小写 str1+str2 字符串通过'+ ...
- python基础第三章选择结构答案-python3 学习笔记(二)选择结构、循环结构
python3 学习笔记 python 优雅 明确 简单 1.选择结构 (1)简单判断 if else 使用格式: if 条件: 表达式1 else: 表达式2 (2)多条件判断 elif 使用格式 ...
最新文章
- 【Python】Python语言学习:pip工具使用知识,模型保存pickle,PDF与docx相互转换处理...
- python怎么打印字典_在python中打印字典的原始输入顺序
- MMDetection-简介
- ACM 杰出会员姬水旺:量子化学和物理的深度学习
- mysql route mycat_mycat
- 关于【apache- tomcat- 5.5.15/conf /Catalina/localhost配置虚拟目录】时的一些问题。(配置web项目的方式不止一种,虚拟目录就是一个)
- Asp.Net Core 第03局:Startup
- python3.9出了吗_Python 3.9正式版,新特性提前一睹为快
- java checked异常有那些_JAVA 的checked异常和unchecked异常
- 数据集:男女身高体重(二维)
- 基于DDPG的智能交通灯控制算法
- 使用docx4j生成数据库字典文档
- si4463 WDS配置参数详解
- Xp计算机同步时间,windows xp时间不准不能自动同步的解决办法
- k8s教程01(k8s环境配置及私有仓库搭建)
- 阿里云总线CSB的HTTP调用案例
- clustalw序列比对_你还在用ClustalW做多序列比对?OUT了
- OpenCV训练分类器制作xml文档
- 程序员与颈椎病(一) 我得了什么病
- Linux的常用命令思维导图
热门文章
- 听歌学英语1 pocketful of sunshine
- 菜鸟来也!50行Python代码一键整理桌面
- VPI TransmissionMaker光纤通信算法仿真 均衡,载波相位恢复
- ksweb nat123 手机Android建站解析
- 腾讯短信集成报错误:NoClassDefFoundError: org/apache/http/client/config/RequestConfig
- 连续六年稳居中国SDN(软件)市场份额第一
- 【MarkDown语法以及软件的安装】
- DataFrame保存为excel的方法
- 智能语音电话机器人怎么选?从这三方面下手
- 阿里云CDN+点播服务助力云学堂全面提升用户在线学习体验