python3 从网页上爬取图片

版权声明:本文为博主原创文章,请尊重原创,转载请注明原文地址和作者信息! https://blog.csdn.net/zzc15806/article/details/81666756
#-*- coding: UTF-8 -*-
#!/usr/python3
import urllib.request
import re
def getImage(url):
    html = urllib.request.urlopen(url).read()   # 爬取网页
    imgre = re.compile(r'src="(.+?\.jpg)"')   #匹配图片
    html = html.decode('utf-8') 
    imglist = imgre.findall(html)
    x=0
    for image in imglist:
        urllib.request.urlretrieve(image,'./image/%s.jpg' % x)
        x+=1
print(getImage("https://www.csdn.net/"))

可能遇到的问题:

1. AttributeError: module 'urllib' has no attribute 'urlopen'
解决办法:将urllib改成urllib.request
2. TypeError: cannot use a string pattern on a bytes-like object
解决办法:python3中需要使用html = html.decode('utf-8') 进行转化

猜你喜欢

转载自blog.csdn.net/zzc15806/article/details/81666756