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 ...
随机推荐
- 本次安装Lion记录
黑苹果驱动之家 http://it360.org.cn/ Mac安装在Macintosh上是没什么好讲的,也没什么难度,原生的东西.但每次Mac安装在PC上总能折腾一番,驱动.五国.各种报错..今天记 ...
- 从乌云的错误漏洞分析看Mifare Classic安全
前言 12年2月初国内著名安全问题反馈平台-乌云发布了有关某公司员工卡的金额效验算法破解的安全问题.从整个漏洞分析来看,漏洞的提交者把员工卡的数据分析得非常仔细,以至很多刚刚接触或者未曾接触的都纷纷赞 ...
- windows下wim配置成IDE
1.配置文件_wimrc set fileencodings=utf-,ucs-bom,cp936,big5 set fileencoding=utf- source $VIMRUNTIME/vimr ...
- 基于jQuery的时间轴鼠标悬停动画插件
之前为大家分享了很多jquery插件,这次我们要来分享一款不太常见的jQuery插件,它是一个时间轴,时间轴上的每一个点在鼠标滑过时都可以显示该点的描述信息,并且鼠标滑过时都可以产生一定的动画特效,比 ...
- Psql 安装问题
在openerp安装过程中报错: psql: could not connect to server: No such file or directory Is the server running ...
- 深入分析DDR(转载)
深入分析:我们为何需要DDR2内存技术 http://www.cnblogs.com/thx-bj/archive/2008/04/02/1134040.html 文/IT168评测室特约 Myddn ...
- C++中变量做数组长度
在Java中,这是完全可以的,比如我们运行如下程序: package cn.darrenchan.storm; import java.util.Arrays; public class Test { ...
- 专题实验 Toad 用户的创建与管理( 包括 role 等 )
1. 用户登录数据库 是否可以通过操作系统权限来登录数据库, $ORACLE_HOME/network/admin/sqlnet.ora 这个文件中设置, 如果增加参数sqlnet.authentic ...
- Linux 复制、移动覆盖文件不提示
# vi ~/.bashrc 如果你看到如下内容,以下命令都会用别名执行了,就是说自动加了 -i 参数 alias rm='rm -i'alias cp='cp -i'alias mv='mv - ...
- Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function问题解决
selenium 目录下的lib文件夹下的所有包都加到类库里