无比强大!Python抓取cssmoban网站的模版并下载

Python实现抓取http://www.cssmoban.com/cssthemes网站的模版并下载


实现代码

# -*- coding: utf-8 -*-
import urlparse
import urllib2
import re
import os  
import os.path

URL='http://www.cssmoban.com/cssthemes'

#全局超时设置 
urllib2.socket.setdefaulttimeout(500)

#根据url获取内容
def getUrlContent(url):
    response = urllib2.urlopen(url)
    html = response.read();
    return html

#获取html中的a标签,且格式是<a target="_blank" href="/showcase/*">的
def getAllUrl(html):
    return re.findall('<a[\\s]+href="/cssthemes/\d+\.shtml">.*?\/a>',html)

#获取下载文件的标题
def getDownTitle(html):
    return re.findall('\<h1>(.*?)\</h1>',html)

#获取文件下载的url
def getDownUrl(html):
    return re.findall('<a.*?class="button btn-down".*?\/a>',html)

#获取下一页的url
def getNextUrl(html):
    return re.findall('<a.*?下一页</a>',html)

#下载文件
def download(title,url):
    result = urllib2.urlopen(url).read()
    if os.path.exists("template/")==False:
        os.makedirs("template/")
    newname=("template/"+title.decode('utf-8'))
    newname=newname+'.'+url[url.rfind('.')+1:len(url)]
    open(newname, "wb").write(result)

#记录日志
def i(msg):
    fileobj=open('info.log','a')
    fileobj.write(msg+'\n')
    fileobj.close();
    print msg
#记录错误日志
def e(msg):
    fileobj=open('error.log','a')
    fileobj.write(msg+'\n')
    fileobj.close();
    print msg
if __name__ == '__main__':

    #print getDownUrl('<a href="http://down.cssmoban.com/cssthemes1/cctp_17_jeans.zip" target="_blank" class="button btn-down" title="免费下载"><i class="icon-down icon-white"></i><i class="icon-white icon-down-transiton"></i>免费下载</a>')
    
    html= getUrlContent(URL)
    i('开始下载:%s' %(URL))
    while True:
        lista= getAllUrl(html);
        #print lista;
        nextPage=getNextUrl(html)
        #print nextPage[0]
        nextUrl=''
        #i('下一页%s'%(nextPage))
        
        if len(nextPage)<=0:
            e('地址:%s,未找到下一页,程序退出' %(nextPage))
            break;
        
        nextUrl=nextPage[0]
        nextUrl=URL+'/'+nextUrl[nextUrl.index('href="')+6:nextUrl.index('" target')]
        #print nextPage
        for a in lista:
            downGotoUrl=''
            try:
                #print a.decode('utf-8')
                downGotoUrl=(URL+''+a[a.index('href="')+6:a.index('">')])
                downGotoUrl=downGotoUrl.replace(URL,'http://www.cssmoban.com')
                #print downGotoUrl
                downHtml=getUrlContent(downGotoUrl)
                #print downHtml
                downTitleList= getDownTitle(downHtml)
                downTitle=''
                if len(downTitleList)>0:
                    downTitle=downTitleList[0]
                #print downTitle
                downUrlList= getDownUrl(downHtml)
                downUrl=''
                if len(downUrlList)>0:
                    downUrl=downUrlList[0]
                downUrl= downUrl[downUrl.index('href="')+6:downUrl.index('" target')]
                #print downUrl
                i('开始下载:%s,文件名:%s' %(downUrl,downTitle))

                download(downTitle,downUrl)
                i('%s下载完成,保存文件名:%s' %(downUrl,downTitle))
            except Exception,e:
                e('地址:%s下载失败,失败信息:' %(downGotoUrl))
                e(str(e))
                

        i('-----------------------------------------')
        i('执行下一页:%s' %(nextUrl))
        html= getUrlContent(nextUrl)
    
        


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫大叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值