python3 学习(2):在网站地图爬虫时的cannot use a string pattern on a bytes-like object 问题的解决方法...
python3.6.5 + pycharm
注意:
一、python3里的 urllib2 已经没有了,改为了 urllbi.request,因此,直接导入 import urllib.request 即可。
二、必须对正则表达式里的引用变量进行格式变换 .decode('utf-8'),否则会报错说 不能在一个字节类的对象上使用字符串格式。
如下代码所示。
### 一、 网站地图爬虫,控制用户代理的设定,可捕获异常、重试下载并设置用户代理。wswp: web scraping with python 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') ## must add this . 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 Rocky_crawl_sitemap('http://example.webscraping.com/sitemap.xml')
运行结果报错:
Downloading: http://example.webscraping.com/sitemap.xml
Traceback (most recent call last):
File "C:/Users/klooa/my_env/book9/test.py", line 25, in <module>
Rocky_crawl_sitemap('http://example.webscraping.com/sitemap.xml')
File "C:/Users/klooa/my_env/book9/test.py", line 22, in Rocky_crawl_sitemap
links = re.findall('<loc>(.*?)</loc>', sitemap) ## extract the sitemap links from flag loc
File "C:\Users\klooa\AppData\Local\Programs\Python\Python36\lib\re.py", line 222, in findall
return _compile(pattern, flags).findall(string)
TypeError: cannot use a string pattern on a bytes-like object
必须在 sitemap的下一行加上
sitemap = sitemap.decode('utf-8')
修改后的运行结果为:
Downloading: http://example.webscraping.com/sitemap.xml
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
Downloading: http://example.webscraping.com/places/default/view/Anguilla-8
Downloading: http://example.webscraping.com/places/default/view/Antarctica-9
Downloading: http://example.webscraping.com/places/default/view/Antigua-and-Barbuda-10
Downloading: http://example.webscraping.com/places/default/view/Argentina-11
Downloading: http://example.webscraping.com/places/default/view/Armenia-12
Download error: TOO MANY REQUESTS
Downloading: http://example.webscraping.com/places/default/view/Aruba-13
Download error: TOO MANY REQUESTS
……
## -- written by LiSongbo
转载于:https://www.cnblogs.com/LiSongbo/p/9245449.html
python3 学习(2):在网站地图爬虫时的cannot use a string pattern on a bytes-like object 问题的解决方法...相关推荐
- python3学习(5):在网站地图爬虫时的cannot use a string pattern on a bytes-like object 问题的解决方法
一.python3里的 urllib2 已经没有了,改为了 urllbi.request,因此,直接导入 import urllib.request 即可. 二.必须对正则表达式里的引用变量进行格式变 ...
- 在安装project2010 64位时提示 “无法安装64位office,因为已有32位版本”解决方法
在安装project2010 64位时提示 "无法安装64位office,因为已有32位版本"解决方法 参考文章: (1)在安装project2010 64位时提示 "无 ...
- Mysql登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES)无法打开的解决方法
MySQL登录时出现Access denied for user 'root'@'localhost' (using password: YES)无法打开的解决方法 本人配置:系统64位win10,M ...
- Mac连Wi-Fi时显示:“wifi有自分配的ip地址将无法接入互联网”解决方法
Mac连Wi-Fi时显示:"wifi有自分配的ip地址将无法接入互联网" 解决方法: 打开网络偏好设置 选择高级模式 点击[Wi-Fi],找到现在连接的无法上网的Wi-Fi,点击[ ...
- 计算机组装时遇到的问题,计算机组装及正常维修过程中遇到的问题和解决方法.doc...
<计算机组装及正常维护过程中遇到的问题和解决方法> 指导老师:XXX 班级:XXX 姓名:XXX 学号:XXXX 计算机组装遇到的问题和解决方法 在安装之前要特别注意下面这些事项:第一.防 ...
- 安装CentOS 时找不到硬盘( no usable disks have been found)的解决方法
电脑主板是昂达N78c,日立硬盘,安装CentOS 6.3 安装时找不到硬盘,百度,google搜索各种方法. 有让更改SATA硬盘模式为IDE或兼容模式,但我的电脑中的BIOS中没有这些选项,只有一 ...
- C语言编辑时光标一直闪,win7系统编辑文字鼠标光标一直闪烁问题的解决方法 - win7吧...
win7系统编辑文字鼠标光标一直闪烁问题的解决方法 使用电脑的过程中我们肯定是会进行文字输入的,不管是聊天游戏还是网页访问我们都会必要的进行一些文字编辑,但是有用户在对文字进行编辑时却遇到鼠标光标 一 ...
- python3报错 TypeError: can’t concat bytes to str 原因与解决方法
在做项目的时候,Python3会报错如下错误: TypeError: can't concat bytes to str 意思是: 类型错误:无法将字节连接到字符串 类似的错误有: TypeError ...
- [UE4]打包运行时提示Plugin ‘‘ failed to load because module ‘‘ could not be found.缺少插件解决方法
提示错误如下 Plugin 'Dialoqueplugin' failed to load because module 'DialoguePlugin' could not be found. Pl ...
最新文章
- UIView 弹出动画
- Ubuntu 12.04 64bit 安装编译GCC 4.1.2 绝对原创
- Oracle数据库相关
- python元胞自动机模拟交通_大师兄带你复现 -gt; 难度超高的二维CA元胞自动机模型...
- Elasticsearch 之(24)IK分词器配置文件讲解以及自定义词库
- 腾讯发现Google Home首个无接触攻破漏洞
- 用碧海潮声制作的宋体(雅黑宋体)替换Windows7原生的火柴棍式的宋体
- 实践分享:开始用Cordova+Ionic+AngularJS开发App
- MOON.ORM 3.5 MYSQL的配置及使用方法(最新版免费下载使用.欢迎加盟)
- python连接mysql的一些基础知识+安装Navicat可视化数据库+flask_sqlalchemy写数据库
- c语言线程不安全错误定位,C语言中的线程安全可破坏事件触发类#
- 试题3 基础练习 数列排序
- 《MYSQL必知必会》—1.了解SQL
- 请求到达时先经过过滤器还是拦截器_上海自动卷绕式过滤器价格_康斐净化
- 微信小程序生成体验版的二维码
- 网站html源代码如何修改,如何修改网页源代码
- 盛大是中国互联网最耀眼的流星
- python:计算自己的bmi值
- 简单抽奖机系统(中午吃什么?)
- 组装一台稳定的计算机
热门文章
- API、JSON等知识的初步学习,以及关于Quake Report应用的总结,以后开发可以用到的一些技能
- 中国剩余定理——孙子定理
- 合肥工业大学计算机与信息学院张勇,合肥工业大学计算机与信息学院导师教师师资介绍简介-△王昱洁...
- mongo vue 破解
- bzoj1106[POI2007]立方体大作战tet*
- 10.EVE-NG镜像来啦!打造国内最大的EVE交流圈
- freeswitch对接ims vos 众方 鼎信通达等网关的模版
- SHELL的脚本编写(2)
- Excel - VBA实例: 遍历若干cell的值
- vue 双问号语法编译报错