python3学习(5):在网站地图爬虫时的cannot use a string pattern on a bytes-like object 问题的解决方法
一、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 = None if 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
……
python3学习(5):在网站地图爬虫时的cannot use a string pattern on a bytes-like object 问题的解决方法相关推荐
- python3 学习(2):在网站地图爬虫时的cannot use a string pattern on a bytes-like object 问题的解决方法...
python3.6.5 + pycharm 注意: 一.python3里的 urllib2 已经没有了,改为了 urllbi.request,因此,直接导入 import urllib.reques ...
- 在安装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 ...
最新文章
- ES6的十大特性和认知
- 大数系列之大数相加(ACM通过)(一)
- Jquery基础知识
- php中如何配置环境变量,如何配置phpstorm环境变量如何配置phpstorm环境变量
- C# DateTime类
- “玲珑杯”ACM比赛 Round #24: C. この戦いが終わったら(BFS+bitset优化暴力)
- springboot之websocket集成
- python网络编程百度云_python网络编程.mobi
- D触发器/同步异步复位/异步复位同步释放详细解释
- SketchUp插件|ClothWorks布料模拟 免费下载!(草图大师制作窗帘张拉膜等)
- “菁客”发布《2018中国移动社交招聘趋势报告》
- 可恶的零宽空格—ZWSP
- Java开发 微软OAuth身份验证在EWS中的应用
- 结合NAACL2022对计算语言学趋势的思考与分析
- Linux常用命令——mailq命令
- xmmi - 晨星半导体公司MSTAR平台
- React Native 手势触摸事件机制详解(基础篇)
- JQuery 图片滚动或者div滚屏,适合多图轮播
- H323——460NAT穿透相关
- “流量水果”——牛油果的营销骗局