无比强大!Python抓取cssmoban站点的模版并下载
2017-08-06 08:07
295 查看
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)
相关文章推荐
- 无比强大!Python抓取cssmoban网站的模版并下载
- 使用Python编写简单网络爬虫抓取视频下载资源
- python抓取网站的图片并下载到本地的方法
- 织梦精仿AB模版网独家修复版 修复图片链接 强大VIP用户中心+手机版 可完美运营下载
- 【Python3】抓取Github吉祥物Octocat昵图并下载到本地
- python 抓取今日头条街拍图片并下载到本地
- 使用Python编写简单网络爬虫抓取视频下载资源
- 基于python3 抓取贴吧图片与评论 图片下载保存
- python-抓取数据、下载图片(正则)、构造headers、urlencode、get_post
- Python抓取网页&批量下载文件方法初探(正则表达式+BeautifulSoup) (转)
- python 爬虫(二)一个带下载进度的网页抓取小爬虫
- Python抓取百度贴吧网页信息以及代码下载
- 53套SharePoint 2010站点模版在线演示及下载
- python 下载整个站点
- 使用Python编写简单网络爬虫抓取视频下载资源
- 20多个基于CSS设计的免费站点模版下载资源
- python抓取某个网站的教学视频下载并改名
- WebLech是一个功能强大的Web站点下载与镜像工具
- WebLech是一个功能强大的Web站点下载与镜像工具
- 抓取搜狗壁纸公园的图片(四)改用python3.5下载真正的壁纸