python3爬虫下载图片之常见问题

 

刚开始学python3,百度了下python的用法,看到爬虫下载图片很好玩,于是百度各种资料学习了下,结果总是运行不成功,最后终于改好了,记录下。

from urllib import request
import urllib
import re

def getHtml(url):
    page = urllib.request.urlopen(url)
    html = page.read()
    return html

def getImg(html):
    reg = r'src="(.+?\.jpg)"'
    imgre = re.compile(reg)
    html=html.decode('utf-8')#不写此行会报错:TypeError: cannot use a string pattern on a bytes-like object
    imglist = re.findall(imgre,html)
    x = 1
    for imgurl in imglist:
        urllib.request.urlretrieve(imgurl,'C:/Users/zx/Desktop/images/%s.jpg' % x)
        x+=1
    return imglist

html = getHtml("http://www.win4000.com/wallpaper_2358_0_10_1.html")
print(getImg(html))

标红的是python3和python2的不同之处,如不写则会报错:AttributeError: module 'urllib' has no attribute 'urlopen'

如果返回[ ],可能是正则表达式不对

猜你喜欢

转载自www.cnblogs.com/chengyuyu/p/9147730.html
今日推荐