#-coding:utf-8
imoprturllib2
importurllib
importre
#填写需要采集的网址
urlPath='
#设置网页头部信息,模拟浏览器
headers={'User-Agent':agent,'Accept':'*/*','Referer':''}
#打开网页,并读取网页源码
request=urllib2.Request(urlPath,headers=headers)
response=urllib2.urlopen(request)
html=response.read()
#构建图片标签正则表达式
img=pile(r"""""",re.I)
#保存的图片名称和路径,需要自己设置
path='~/Code/Python/img_splider/'
try:
#使用正则匹配出所有的img标签
img_list=re.findAll(img,html)
#遍历得到的所有标签,然后进行下载
foriinxrange(length(img_list)):
#使用urllib读取打开图片
data=urllib.urlopen(img_list[i]).read()
#保存图片的格式为jpeg,需要自己看
imgStr=path+str(i)+'.jpeg'
#打印出图片的url
printimgStr
#创建文件
f=file(imgStr,'wb')
#写入图片
f.write(data)
#关闭文件流
f.close()
exceptException,e:
print'error'
以前写了很多关于爬虫的文章,在博客上面,,可以参考一下。一起学习,上面的代码可以先试下,不行的话在调试