首页 > 代码库 > python学习笔记-抓取网页图片脚本

python学习笔记-抓取网页图片脚本

初学者一枚,代码都是模仿网上的。亲测可用~

运行脚本的前提是本机安装了httplib2模块

#!/usr/bin/python
import os
import re
import string
import urllib
#author:reed
#date:2014-05-14
def GetWebPictures():
    url=raw_input(‘please input the website you want to download:‘)
    imgcontent=urllib.urlopen(url).read()
    urllist=re.findall(r‘src="http://www.mamicode.com/(http.+?\.jpg)"‘,imgcontent,re.I)
    if not urllist:
        print ‘not found...‘
    else:
        filepath=os.getcwd()+‘\img1‘
        if os.path.exists(filepath) is False:
            os.mkdir(filepath)
        x=1
        print ‘begin to get pictures...‘
        for imgurl in urllist:
            temp=filepath+‘\%s.jpg‘ % x
            print ‘it is downloading the %s pictures‘% x
            print imgurl
            urllib.urlretrieve(imgurl,temp)
            x+=1
    print ‘download completely...is saved at ‘+filepath
GetWebPictures()


本文出自 “[root@卢伟开51cto博客~]#” 博客,请务必保留此出处http://luweikai.blog.51cto.com/1705672/1411363