python实现简单抓图并打包成exe

打包:

在CMD命令行运行:python setup.py py2exe

注意:
1.需要安装py2exe
2.更改setup.py中对相应的py文件进行打包
3.打包后包括exe文件在内的必须文件都在dist目录下
4.用命令行跑时,应先将路径cd到py文件的目录下


setup.py:

#!/usr/bin/env python
#coding=utf-8

from distutils.core import setup 
import py2exe 

file="python_oschina_picture_new.py"#.decode('utf-8')  
setup(console=[file]) 


python代码:(python_oschina_picture_new.py)

#!/usr/bin/env python
#coding=utf-8

 
import urllib
import urllib2
import os
import re
import sys
BaseURL='http://www.mzitu.com/'   #首页
Module=['model']                  #需要下载的模块


#显示下载进度
def schedule(a,b,c):
    """
    a:已经下载的数据块
    b:数据块的大小
    c:远程文件的大小
    """
    per = 100.0 * a * b / c
    if per > 100 :
        per = 100
    print '%.2f%%' % per


def GetHtml(URL):
    #req = urllib2.Request('http://192.168.1.2/') 
    #req.add_header('User-agent', 'Mozilla 5.10') #python使用自定义user-agent抓取网页
    #content=urllib2.urlopen(req).read()#.decode('utf-8')    #解码后打印网页内容不乱码,也可以后面再处理乱码情况s
    content=urllib.urlopen(URL).read()
    return content


#获取资源页数
def GetPage(URL,content,flag):
    if flag==0:
        regex=URL+'/page/(\d*)\s*\'\s*>\s*<span>\s*\d*\s*</span>'   #<a class='page-numbers' href='http://www.mzitu.com/model/page/47'><span>47</span></a>
    elif flag==1:
        regex='span\s*class\s*=\s*\'dots\'.*?</span>\s*<\s*a\s*href\s*=\s*\'\s*'+URL+'/(\d*)\'>'    #<span class='dots'>…</span><a href='http://www.mzitu.com/41633/30'>
   
    res=re.findall(regex,content,re.S)          #re.S 表示多行匹配
    return res.pop()


#获取列表
def GetList(content):
    regex=BaseURL+'(\d*)\"\s*title\s*=\s*\"(.*?)\"\s*target\s*=\s*\"_blank\s*\">.*?</a>\s*</h2>' #"http://www.mzitu.com/41107" title="女神许诺白皙翘臀美腿秒杀宅男" target="_blank">女神许诺白皙翘臀美腿秒杀宅男</a></h2>
    list=re.findall(regex,content,re.S)         #re.S 表示多行匹配
    return list


#下载资源
def Download(URL,num,dirName):
    Dir='%s' % unicode(dirName,'utf-8') #对中文(需要用UNICODE显示的字符)进行重新编码
    try:
        if not os.path.exists(Dir):     #路径不存在时创建一个
            os.makedirs(Dir)
        picture = Dir+'/%s.jpg' % num
        if os.path.exists(picture):
            return
        regex='href\s*=\s*\"'+URL+'/{0,1}\d*\".*?<\s*img\s*src\s*=\s*\"(.*?)\"\s*alt' #<p><a href="http://www.mzitu.com/41633/3" ><img src="http://pic.dofay.com/2015/05/27t02.jpg" alt="长发红唇性感睡衣 美女夏瑶秒杀宅男" /></a></p>
        if (int(num))==1:
            htmlURL=URL
        else:
            htmlURL=URL+'/'+num
        url=re.findall(regex,GetHtml(htmlURL),re.S)
        print 'downloading',picture
        #urllib.urlretrieve(url[0],picture,schedule)   #显示下载进度
        urllib.urlretrieve(url[0],picture)             #不显示下载进度
    except:
        print 'exception!','when download',Dir,num+'.jpg'


#下载资源
def Handle(URL):
    content=GetHtml(URL)
    for page in range(1,int(GetPage(URL,content,0))+1):
        if page==1:
            url=URL
        else:
            url=URL+'/page/'+str(page)
            content=GetHtml(url)
        list=GetList(content)
        #for x in list:
            #file=open('E:/study/python/study/picture.txt','a')
            #file.write(x[0]+'\n')
        for L in list:
            URL=BaseURL+L[0]
            content=GetHtml(URL)
            for page_jpg in range(1,int(GetPage(URL,content,1))+1):
                Download(URL,str(page_jpg),str(L[1]))   




if __name__=='__main__':
    for list in Module:
        Handle(BaseURL+list)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值