今天练习使用Python爬取网站图片,遇到些小问题,求证后,将经验分享下 :-)
原来的代码:
import urllib
import re
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getImg(html):
html = html.decode('utf-8')
reg = r'src="(.+?\.jpg)" pic_ext'
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl,'%s.jpg' % x)
x+=1
print('正在打印第%s张图片' % x)
html = getHtml("http://tieba.baidu.com/p/2460150866")
getImg(html)
执行时报错:
Traceback (most recent call last):
File "E:/wenjian/python/python2/wenjianjia/file.py", line 51, in <module>
html = getHtml("http://tieba.baidu.com/p/2460150866")
File "E:/wenjian/python/python2/wenjianjia/file.py", line 37, in getHtml
page = urllib.urlopen(url)
AttributeError: module 'urllib' has no attribute 'urlopen'
Process finished with exit code 1
原因是在Python3中不能直接调用import urllib,应改为import urllib.request
修改后如下:
import urllib.request
import re
#获取网页源码
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html
#使用正则将图片筛选出来
def getImg(html):
html = html.decode('utf-8')
#筛选.jpg格式并以pic_ext结尾的
reg = r'src="(.+?\.jpg)" pic_ext'
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
urllib.request.urlretrieve(imgurl,'%s.jpg' % x)
x+=1
print('正在打印第%s张图片' % x)
html = getHtml("http://tieba.baidu.com/p/2460150866")
getImg(html)
输出结果如下:

浙公网安备 33010602011771号