1. #!/usr/python3
  2. import re
  3. import urllib.request
  4. def gethtml(url):
  5. page=urllib.request.urlopen(url)
  6. html=page.read()
  7. return html
  8. def getimg(html):
  9. reg = r'src="(.*?\.jpg)"'
  10. img=re.compile(reg)
  11. html=html.decode('utf-8') # python3
  12. imglist=re.findall(img,html)
  13. x = 0
  14. for imgurl in imglist:
  15. urllib.request.urlretrieve(imgurl,'%s.jpg'%x)
  16. x = x+1
  17. html=gethtml("http://news.ifeng.com/a/20161115/50243265.html")
  18.  
  19. print(getimg(html))

代码中红色字体部分均为Python3.0及以上版本在学到爬虫是需要注意的,如果没有这些红色的代码的话可能会出现以下情况:

1.TypeError: cannot use a string pattern on a bytes-like object  这种情况解决方法就是加上html=html.decode('utf-8')#python3这句代码;

2.AttributeError: module 'urllib' has no attribute 'urlopen'这种情况的解决办法就是将urllib改成urllib.request就行了。

TypeError: cannot use a string pattern on a bytes-like object的解决办法的更多相关文章

  1. TypeError: cannot use a string pattern on a bytes-like object

    一劳永逸解决:TypeError: cannot use a string pattern on a bytes-like object TypeError: cannot use a string ...

  2. 爬虫python3:TypeError: cannot use a string pattern on a bytes-like object

    import re from common_p3 import download def crawl_sitemap(url): sitemap = download(url) links = re. ...

  3. Cannot get a STRING value from a NUMERIC cell问题的解决办法

    遇到以下错误的解决办法: 在cell加个setCellType()方法就可以了 cell.setCellType(CellType.STRING);

  4. elastic search 日期为string类型导致视图无法展示时间的解决办法

    尝试将结构化的json数据发送到es(elastic search)上,然后创建视图,这样就能以小时维度查看数据,直接使用post发送到es后,创建索引,结果提示 没有date类型的字段(field) ...

  5. String or binary data would be truncated 异常解决办法 .

    原因:一般出现这个问题是因为数据库中的某个字段的长度小,而插入数据大解决:修改表结构,使表字段大小相同或大于要插入的数据

  6. Symbols of String Pattern Matching

    Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...

  7. gulp 打包错误 TypeError: Path must be string. Received undefined

    Running gulp gives “path.js:7 throw new TypeError('Path must be a string. Received ' + inspect(path) ...

  8. Python 出现 can't use a string pattern on a bytes-like object

    Python 出现 can't use a string pattern on a bytes-like object 学习了:https://www.cnblogs.com/andrewleeeee ...

  9. python3 pycurl 出现 TypeError: string argument expected, got 'bytes' 解决方案

    用pycurl请求指定链接并返回结果时出现 TypeError: string argument expected, got 'bytes'  错误 经过排查问题出现在使用StringIO的write ...

随机推荐

  1. request的生存期只限于服务器跳转

    症状: 刚才想做一个实验,在a.jsp中向request添加属性(页面编码为UTF-8),在b.jsp中删除该属性(页面编码为ISO-8859-1),通过ServletRequestAttribute ...

  2. 解决VisualStudio2013无法查看数组内容的问题

    症状: 在使用VS2013调试的时候,数组只能查看第一个元素的值.如图 解决方案: 调试>窗口>内存 输入数组的内存地址,右击内存窗口>带符号显示(也可以选择16进制显示,看你自己的 ...

  3. select下拉选框的默认值,包括每次进入页面的默认值

    下拉选: <select onchange="selectTotal(this.value)" style="width: 50px;">      ...

  4. ComBoFuzzySearch.js

    /** * combobox和combotree模糊查询 */(function () { //combobox可编辑,自定义模糊查询 $.fn.combobox.defaults.editable ...

  5. jenkins环境搭建&配置(二)

    Jenkins介绍: Jenkins用于监控持续重复的工作,功能包括: 1.持续的软件版本发布/测试项目. 2.监控外部调用执行的工作. 安装环境: 操作系统:linux(centOS) 软件:jdk ...

  6. imx6 MfgTool分析

    解析freescale的MfgTool中的脚本,了解imx6, android系统的分区情况. 配置文件 1. cfg.ini [profiles] chip = MX6DL Linux Update ...

  7. Struts2的拦截器是如何使用AOP工作的

    拦截器(interceptor)是Struts2最强大的特性之一,也可以说是struts2的核心,拦截器可以让你在Action和result被执行之前或之后进行一些处理.同时,拦截器也可以让你将通用的 ...

  8. 【cf489】D. Unbearable Controversy of Being(暴力)

    http://codeforces.com/contest/489/problem/D 很显然,我们只需要找对于每个点能到达的深度为3的点的路径的数量,那么对于一个深度为3的点,如果有a种方式到达,那 ...

  9. 基于docker部署的微服务架构(四): 配置中心

    原文:http://www.jianshu.com/p/b17d65934b58%20 前言 在微服务架构中,由于服务数量众多,如果使用传统的配置文件管理方式,配置文件分散在各个项目中,不易于集中管理 ...

  10. php7垃圾回收分析