#-*- 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') 进行转化