码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-05-15 09:03:42      阅读:385      评论:0      收藏:0      [点我收藏+]

标签: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()


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

python学习笔记-抓取网页图片脚本,布布扣,bubuko.com

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

标签:python

原文地址:http://luweikai.blog.51cto.com/1705672/1411363

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!