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站点的模版并下载的更多相关文章

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

    Python实现抓取http://www.cssmoban.com/cssthemes网站的模版并下载 实现代码 # -*- coding: utf-8 -*- import urlparse imp ...

  2. Python抓取视频内容

    Python抓取视频内容 Python 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年.Python语法简洁而清晰,具 ...

  3. Python抓取页面中超链接(URL)的三中方法比较(HTMLParser、pyquery、正则表达式) <转>

    Python抓取页面中超链接(URL)的3中方法比较(HTMLParser.pyquery.正则表达式) HTMLParser版: #!/usr/bin/python # -*- coding: UT ...

  4. 使用Python抓取猫眼近10万条评论并分析

    <一出好戏>讲述人性,使用Python抓取猫眼近10万条评论并分析,一起揭秘“这出好戏”到底如何? 黄渤首次导演的电影<一出好戏>自8月10日在全国上映,至今已有10天,其主演 ...

  5. Python 抓取网页并提取信息(程序详解)

    最近因项目需要用到python处理网页,因此学习相关知识.下面程序使用python抓取网页并提取信息,具体内容如下: #---------------------------------------- ...

  6. 使用 Python 抓取欧洲足球联赛数据

    Web Scraping在大数据时代,一切都要用数据来说话,大数据处理的过程一般需要经过以下的几个步骤    数据的采集和获取    数据的清洗,抽取,变形和装载    数据的分析,探索和预测    ...

  7. python抓取性感尤物美女图

    由于是只用标准库,装了python3运行本代码就能下载到多多的美女图... 写出代码前面部分的时候,我意识到自己的函数设计错了,强忍继续把代码写完. 测试发现速度一般,200K左右的下载速度,也没有很 ...

  8. python抓取网页例子

    python抓取网页例子 最近在学习python,刚刚完成了一个网页抓取的例子,通过python抓取全世界所有的学校以及学院的数据,并存为xml文件.数据源是人人网. 因为刚学习python,写的代码 ...

  9. 如何用python抓取js生成的数据 - SegmentFault

    如何用python抓取js生成的数据 - SegmentFault 如何用python抓取js生成的数据 1赞 踩 收藏 想写一个爬虫,但是需要抓去的的数据是js生成的,在源代码里看不到,要怎么才能抓 ...

随机推荐

  1. mysql-错误备查

    转载请注明:仰望大牛的小清新   http://www.cnblogs.com/luruiyuan/ 这个文章的主要目的是总结自己的作死经历,以备查找 1. Ubuntu MySQL 服务的启动/停止 ...

  2. 字符串Hash相关

    其实也并不是什么特别难的算法,但是我个人实在是不太喜欢字符串之类的东西(字符串神马的真的是麻烦),于是一直拖着不想看,然后模板题之类的也懒得做. Hash的思想其实也没什么复杂的,就是给定一系列字符串 ...

  3. Linux命令之ping

    ping [选项] destination ping命令向网络主机发送ICMP回传请求 详细描述:ping使用ICMP协议强制ECHO_REQUEST(回传请求)数据报从主机或网关获取ICMP协议的E ...

  4. ABP+NetCore+Vue.js实现增删改查

    ABP我就不多介绍了,不知道的可以自己百度 本篇开发工具VS2017,数据库SQL SERVER2012,系统Win7 1.去ABP官网下载对应的模板,下载地址:https://aspnetboile ...

  5. [TCO2009]NumberGraph

    题意:给你一些带权的节点和一个正整数集合$S$,$S$中每一个数的二进制后缀$0$个数相同,节点$x$的权值为$v_x$,如果对于$x,y$存在$t\in S$使得$|v_x-v_y|=t$,那么连边 ...

  6. 【强连通分量缩点】【DFS】【动态规划】Urozero Autumn Training Camp 2016 Day 5: NWERC-2016 Problem B. British Menu

    有向图,不经过重复点的最长链,强连通分量大小不超过5. 每个强连通分量内部暴力预处理任意两对点之间的最长路,外面DAG上dp. 不是很好写,但是预处理完了之后,可以重构每个强连通分量内部的结构,然后整 ...

  7. [转]SSH包全解

    Struts2 Core Libraries 必须引入的包 : struts2-core.jar——Struts2的核心包 xwork-core.jar——Command模式框架,WebWork和St ...

  8. Codeforces Round #342 (Div. 2) E. Frog Fights set 模拟

    E. Frog Fights 题目连接: http://www.codeforces.com/contest/625/problem/E Description stap Bender recentl ...

  9. 调整Chrome中文字体为雅黑

    几天试了一下Chrom发布的Chrome 37,感觉所谓的Direct2D渲染还是有些效果的,但这只体现在英文上,中文的宋体还是非常的不舒服,便试着将其替换成了雅黑,这里介绍一下相关方法. Chrom ...

  10. GNU Debugger for Windows----GDB

            This web page provides 32-bit and 64-bit binaries of gdb for Windows for download. Equation ...