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

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

    一.python3里的 urllib2 已经没有了,改为了 urllbi.request,因此,直接导入 import urllib.request 即可. 二.必须对正则表达式里的引用变量进行格式变 ...

  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. 程序员持续成长,需要持久而痛苦的学习
  2. SpringBoot 接口幂等性的实现方案
  3. 什么是加密?—Vecloud微云
  4. 【目录】python全栈工程师自动化+Py全栈+爬虫+Ai+python全栈工程师
  5. java uuid 排序_如何在java中将UUID保存为二进制(16)
  6. C# ObservableCollection和List的区别
  7. HTML5/CSS3基础
  8. python中hub_PyHubWeekly | 第一期:Github上那些值得推荐的Python小工具
  9. messagebox弹窗_从案例入手学Python——检测文件生成并弹窗提醒
  10. sku组合查询算法探索
  11. OSPF实验:OSPF认证
  12. 绑定校园卡服务器没有响应,单击校园卡管理系统常见问题汇总.doc
  13. windows网络编程头文件注意事项
  14. ESP8266与ESP8285开发时有什么区别
  15. 【算法专题】链表排序算法总结
  16. java swfupload 302_SWFUpload 302
  17. R语言 判别分析:线性判别、K最邻近、有权重的K最邻近、朴素贝叶斯
  18. CS61B project 2 示例图的地牢地图房间走廊地图生成洞穴地图生成方法
  19. 【全】常见的空气净化技术,你知道几种类型?
  20. redhat linux 批量改名,CentOS的批量改名命令 rename和prename

热门文章

  1. 变频器主要功率元器件损耗分析
  2. hdu 2648 shoping
  3. vue项目兼容m3u8格式视频,h5的Android播放异常
  4. Vue框架总结(一、Vue基础知识)
  5. SEO查询工具你只知道5118吗?这个工具包值得收藏
  6. Windows服务 FLEXnet Licensing service 64
  7. python 设计模式 观察者模式(发布订阅模式)
  8. 开源项目学习笔记(1)——狗屁不通文章生成器(BullshitGenerator)
  9. 为什么你的问题群里没人回答?这就是原因!
  10. LR(1)语法分析器生成器(生成Action表和Goto表)java实现(二)