一、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 问题的解决方法相关推荐

  1. python3 学习(2):在网站地图爬虫时的cannot use a string pattern on a bytes-like object 问题的解决方法...

    python3.6.5 +  pycharm 注意: 一.python3里的 urllib2 已经没有了,改为了 urllbi.request,因此,直接导入 import urllib.reques ...

  2. 在安装project2010 64位时提示 “无法安装64位office,因为已有32位版本”解决方法

    在安装project2010 64位时提示 "无法安装64位office,因为已有32位版本"解决方法 参考文章: (1)在安装project2010 64位时提示 "无 ...

  3. Mysql登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES)无法打开的解决方法

    MySQL登录时出现Access denied for user 'root'@'localhost' (using password: YES)无法打开的解决方法 本人配置:系统64位win10,M ...

  4. Mac连Wi-Fi时显示:“wifi有自分配的ip地址将无法接入互联网”解决方法

    Mac连Wi-Fi时显示:"wifi有自分配的ip地址将无法接入互联网" 解决方法: 打开网络偏好设置 选择高级模式 点击[Wi-Fi],找到现在连接的无法上网的Wi-Fi,点击[ ...

  5. 计算机组装时遇到的问题,计算机组装及正常维修过程中遇到的问题和解决方法.doc...

    <计算机组装及正常维护过程中遇到的问题和解决方法> 指导老师:XXX 班级:XXX 姓名:XXX 学号:XXXX 计算机组装遇到的问题和解决方法 在安装之前要特别注意下面这些事项:第一.防 ...

  6. 安装CentOS 时找不到硬盘( no usable disks have been found)的解决方法

    电脑主板是昂达N78c,日立硬盘,安装CentOS 6.3 安装时找不到硬盘,百度,google搜索各种方法. 有让更改SATA硬盘模式为IDE或兼容模式,但我的电脑中的BIOS中没有这些选项,只有一 ...

  7. C语言编辑时光标一直闪,win7系统编辑文字鼠标光标一直闪烁问题的解决方法 - win7吧...

    win7系统编辑文字鼠标光标一直闪烁问题的解决方法 使用电脑的过程中我们肯定是会进行文字输入的,不管是聊天游戏还是网页访问我们都会必要的进行一些文字编辑,但是有用户在对文字进行编辑时却遇到鼠标光标 一 ...

  8. python3报错 TypeError: can’t concat bytes to str 原因与解决方法

    在做项目的时候,Python3会报错如下错误: TypeError: can't concat bytes to str 意思是: 类型错误:无法将字节连接到字符串 类似的错误有: TypeError ...

  9. [UE4]打包运行时提示Plugin ‘‘ failed to load because module ‘‘ could not be found.缺少插件解决方法

    提示错误如下 Plugin 'Dialoqueplugin' failed to load because module 'DialoguePlugin' could not be found. Pl ...

最新文章

  1. ES6的十大特性和认知
  2. 大数系列之大数相加(ACM通过)(一)
  3. Jquery基础知识
  4. php中如何配置环境变量,如何配置phpstorm环境变量如何配置phpstorm环境变量
  5. C# DateTime类
  6. “玲珑杯”ACM比赛 Round #24: C. この戦いが終わったら(BFS+bitset优化暴力)
  7. springboot之websocket集成
  8. python网络编程百度云_python网络编程.mobi
  9. D触发器/同步异步复位/异步复位同步释放详细解释
  10. SketchUp插件|ClothWorks布料模拟 免费下载!(草图大师制作窗帘张拉膜等)
  11. “菁客”发布《2018中国移动社交招聘趋势报告》
  12. 可恶的零宽空格—ZWSP
  13. Java开发 微软OAuth身份验证在EWS中的应用
  14. 结合NAACL2022对计算语言学趋势的思考与分析
  15. Linux常用命令——mailq命令
  16. xmmi - 晨星半导体公司MSTAR平台
  17. React Native 手势触摸事件机制详解(基础篇)
  18. JQuery 图片滚动或者div滚屏,适合多图轮播
  19. H323——460NAT穿透相关
  20. “流量水果”——牛油果的营销骗局

热门文章

  1. esp32-pico-d4开发环境配置
  2. SCL语言适用于哪些场景?
  3. linux获取路由器wan口的ip地址,如何查看wan口ip地址_wan口ip地址怎么查?-192路由网...
  4. word中每行的文字如何设置文字在当前行居中对齐
  5. 利用python库stats进行t检验
  6. r语言 c长度,如何在R中设置C堆栈的大小?
  7. 计算机教师帮带情况,教师帮带工作总结
  8. iOS多线程详解:实践篇
  9. C++实现二叉树的递归遍历与非递归遍历
  10. 建筑施工技术【17】