#!/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的解决办法的更多相关文章

  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. poj3261(后缀数组)

    题意:给出一串长度为n的字符,再给出一个k值,要你求重复次数大于等于k次的最长子串长度........ 思路:其实也非常简单,直接求出height值,然后将它分组,二分答案......结果就出来了.. ...

  2. node js 调试出现同一个端口启动多次报错处理方案 Error: listen EADDRINUSE

    windows 下 1.查询端口占用的进程ID: netstat -aon | findstr "80"    80为端口号, 输出为: TCP    0.0.0.0:3000   ...

  3. python walk函数

    os.walk方法 import os for i in os.walk(r'C:\Users\jack\Desktop\test\3_语文语文版七年级上册\1_一单元'): print(i[0]) ...

  4. gcc 编译流程分析

    //test.c #include<stdio.h> int main() { ,y=; printf("x=%d y=%d\n",x,y); ; } 1:预处理阶段, ...

  5. Spider Studio 新版本 (码年吉祥版) - 浏览器视图 / 脚本库上线!

    各位哥哥姐姐弟弟妹妹小伙伴们春节好! 2014年对于我们程序员很重要, 因为今年是 "码" 年! SS在此重要之年到来之际热力推出两大重要功能恭贺新春: 1. 浏览器视图 以前SS ...

  6. UCASE() 函数

    UCASE() 函数 UCASE 函数把字段的值转换为大写. SQL UCASE() 语法 SELECT UCASE(column_name) FROM table_name

  7. PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享

    php屏蔽电话号码中间四位: Method 1: function hidtel($phone){ $IsWhat = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9] ...

  8. Java 执行linux scp 远程获取文件和上传

      需要的jar包:ganymed-ssh2-build210.jar import java.io.ByteArrayOutputStream;import java.io.File;import ...

  9. AWS系列-EC2实例添加磁盘

    注意:添加的磁盘,必须和挂载的实例是在同一可用区. 1.1 如下图,打开EC2控制台,打开卷,点击创建卷 1.2 选择磁盘配置 磁盘类型:如下图 磁盘大小:如图,最小500G,最大16T 可用区:注意 ...

  10. 正则表达式Regex

    1.概念 正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.正则表通常被用来检索.替换那些符合某个模式( ...