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.+?\.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()


你可能感兴趣的:(python)