#!/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. jquery插件-table转Json数据插件

    使 用开源插件Table-to-json: 官方地址:http://lightswitch05.github.io/table-to-json/ 功能说明:将js对象table转换成javascrip ...

  2. 第八课:不一样的链表 linux链表设计哲学 5星级教程

    这一课最后实现的链表,和普通链表不同,借鉴了linux内核链表的思想,这也是企业使用的链表. 基础介绍: 顺序表的思考 顺序表的最大问题是插入和删除需要移动大量的元素!如何解决?A:在线性表数据元素之 ...

  3. FreeRTOS 消息队列

    以下基础内容转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解 FreeRTOS 的一个重要的通信机制----消息队列,初学者要熟练掌握,因为消息 ...

  4. xcode6 dyld_sim is not owned by root

    如果运行复制过来的xcode可能会这个提示,xcode6 dyld_sim is not owned by root解决方法打开终端 输入sudo xcode-select -switch /Appl ...

  5. c# 程序调试出现“未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序。”

    简单的程序代码如下:DataSet ds=new DataSet();try{ string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data ...

  6. Javascript知识点:IIFE - 立即调用函数

    Immediately-invoked Function Expression(IIFE,立即调用函数),简单的理解就是定义完成函数之后立即执行.因此有时候也会被称为“自执行的匿名函数”(self-e ...

  7. Windows 只能安装32位虚拟机问题

    查了一下相关原因,是因为cpu的虚拟化没有打开的原因 解决方法: 进去bios 里面, 进入 configuration , 将 Intel Virtual Technology 设为Enabled ...

  8. CSS——你所不知的 CSS ::before 和 ::after 伪元素用法(转)

    你所不知的 CSS ::before 和 ::after 伪元素用法 CSS 有两个说不上常用的伪类 :before 和 :after,偶尔会被人用来添加些自定义格式什么的,但是它们的功用不仅于此.前 ...

  9. JQuery元素选择

    1.基本元素选择器 $(“p”) //选取<p>元素 $(“p.info”) //选取所有class=”info”的<p>元素 $(“p#demo”) //选取id=”demo ...

  10. 数据库 Navicat_Premium_11.0.10 破解版下载安装

    下载地址:http://www.liangchan.net/soft/download.asp?softid=4785&downid=8&id=4804 破解说明:安装之后不要立即启动 ...