TypeError: cannot use a string pattern on a bytes-like object的解决办法
#!/usr/python3
import re
import urllib.request
def gethtml(url):
page=urllib.request.urlopen(url)
html=page.read()
return html
def getimg(html):
reg = r'src="(.*?\.jpg)"'
img=re.compile(reg)
html=html.decode('utf-8') # python3
imglist=re.findall(img,html)
x = 0
for imgurl in imglist:
urllib.request.urlretrieve(imgurl,'%s.jpg'%x)
x = x+1
html=gethtml("http://news.ifeng.com/a/20161115/50243265.html") 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的解决办法的更多相关文章
- 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 ...
- 爬虫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. ...
- Cannot get a STRING value from a NUMERIC cell问题的解决办法
遇到以下错误的解决办法: 在cell加个setCellType()方法就可以了 cell.setCellType(CellType.STRING);
- elastic search 日期为string类型导致视图无法展示时间的解决办法
尝试将结构化的json数据发送到es(elastic search)上,然后创建视图,这样就能以小时维度查看数据,直接使用post发送到es后,创建索引,结果提示 没有date类型的字段(field) ...
- String or binary data would be truncated 异常解决办法 .
原因:一般出现这个问题是因为数据库中的某个字段的长度小,而插入数据大解决:修改表结构,使表字段大小相同或大于要插入的数据
- Symbols of String Pattern Matching
Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...
- 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) ...
- 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 ...
- python3 pycurl 出现 TypeError: string argument expected, got 'bytes' 解决方案
用pycurl请求指定链接并返回结果时出现 TypeError: string argument expected, got 'bytes' 错误 经过排查问题出现在使用StringIO的write ...
随机推荐
- ubuntu 16.04LTS
安装出现"server64 busybox-initramfs安装失败" 这个是BUG,解决方法一:初次安装选择语言时,使用English,在后面还会有一个选择语言的界面,这时候再 ...
- Git出现error: Your local changes to the following files would be overwritten by merge: ... Please, commit your changes or stash them before you can merge.的问题解决(Git代码冲突)
在使用git pull拉取服务器最新版本时,如果出现error: Your local changes to the following files would be overwritten by m ...
- Makefile学习之路6——让编译环境更加有序
在大多项目中都会合理设计目录结构来提高维护性,在编译一个项目时会产生大量中间文件,如果中间文件直接和源文件放在一起,就显得杂乱而不利于维护.在为现在这个complicated项目编写makefile之 ...
- la4730(并查集+树状数组)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=30& ...
- c#用picturebox显示多页TIF
//引用 using System.Drawing; using System.Drawing.Imaging; //以下是方法 private Bitmap myImage = null; priv ...
- 百度JS模板引擎
1. 应用场景 前端使用的模板系统 或 后端Javascript环境发布页面 2. 功能描述 提供一套模板语法,用户可以写一个模板区块,每次根据传入的数据,生成对应数据产生的HTML片段,渲染不同 ...
- 编译Spark2.1.2源码
源码编译的shell脚本为 /dev/make-distribution.sh ,下载源码包解压就能找到.不同版本使用的参数有差异.可以直接查看make-distribution.sh文件. 下载sp ...
- 1.svn+apache整合
1:安装svn客户端, 并且在客户端下,能建立仓库,在自己的硬盘上,建一个库利用 file:///D:/xx/yy来管理自己的仓库. 2:把svn与apache整合. 2.1为什么与apache一起整 ...
- 二、thinkphp
## ThinkPHP 3.1.2 查询方式#讲师:赵桐正微博:http://weibo.com/zhaotongzheng 本节课大纲:一.普通查询方式 a.字符串 $arr=$m->wher ...
- php if语句判定ms查询是否为空
$select_re_id=$cnx->query("select count(*) c from Yid where pid='".$pid."' and gid ...