无比强大!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)
无比强大!Python抓取cssmoban站点的模版并下载的更多相关文章
- 无比强大!Python抓取cssmoban网站的模版并下载
Python实现抓取http://www.cssmoban.com/cssthemes网站的模版并下载 实现代码 # -*- coding: utf-8 -*- import urlparse imp ...
- Python抓取视频内容
Python抓取视频内容 Python 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年.Python语法简洁而清晰,具 ...
- Python抓取页面中超链接(URL)的三中方法比较(HTMLParser、pyquery、正则表达式) <转>
Python抓取页面中超链接(URL)的3中方法比较(HTMLParser.pyquery.正则表达式) HTMLParser版: #!/usr/bin/python # -*- coding: UT ...
- 使用Python抓取猫眼近10万条评论并分析
<一出好戏>讲述人性,使用Python抓取猫眼近10万条评论并分析,一起揭秘“这出好戏”到底如何? 黄渤首次导演的电影<一出好戏>自8月10日在全国上映,至今已有10天,其主演 ...
- Python 抓取网页并提取信息(程序详解)
最近因项目需要用到python处理网页,因此学习相关知识.下面程序使用python抓取网页并提取信息,具体内容如下: #---------------------------------------- ...
- 使用 Python 抓取欧洲足球联赛数据
Web Scraping在大数据时代,一切都要用数据来说话,大数据处理的过程一般需要经过以下的几个步骤 数据的采集和获取 数据的清洗,抽取,变形和装载 数据的分析,探索和预测 ...
- python抓取性感尤物美女图
由于是只用标准库,装了python3运行本代码就能下载到多多的美女图... 写出代码前面部分的时候,我意识到自己的函数设计错了,强忍继续把代码写完. 测试发现速度一般,200K左右的下载速度,也没有很 ...
- python抓取网页例子
python抓取网页例子 最近在学习python,刚刚完成了一个网页抓取的例子,通过python抓取全世界所有的学校以及学院的数据,并存为xml文件.数据源是人人网. 因为刚学习python,写的代码 ...
- 如何用python抓取js生成的数据 - SegmentFault
如何用python抓取js生成的数据 - SegmentFault 如何用python抓取js生成的数据 1赞 踩 收藏 想写一个爬虫,但是需要抓去的的数据是js生成的,在源代码里看不到,要怎么才能抓 ...
随机推荐
- HDU 3339 In Action【最短路+01背包模板/主要是建模看谁是容量、价值】
Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the n ...
- python中join函数的用法
这个函数可以对字符串按照某种方式进行拼接,比如你要在三个字母中间都添加一个特定字符,就可以用这个函数实现 result = '*'.join(['A','B','C']) print(result) ...
- AutoHotKey 常用脚本
; IMPORTANT INFO ABOUT GETTING STARTED: Lines that start with a ; semicolon, such as this one, are c ...
- 离线情报分析工具CaseFile
离线情报分析工具CaseFile CaseFile是Maltego的姊妹工具,功能非常类似于Maltego.CaseFile主要针对数据进行离线分析,缺少Maltego的数据采集功能.它可以导入各 ...
- 【DFS】佳佳的魔法阵
[vijos1284]佳佳的魔法阵 背景 也许是为了捕捉猎物(捕捉MM?),也许是因为其它原因,总之,佳佳准备设计一个魔法阵.而设计魔法阵涉及到的最关键问题,似乎就是那些带有魔力的宝石的摆放…… 描述 ...
- BZOJ 1711:[Usaco2007 Open]Dining吃饭(最大流)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1711 [题目大意] 每头牛都有一些喜欢的饮料和食物, 现在有一些食物和饮料,但是每样只 ...
- 【强连通分量缩点】【拓扑排序】【dp预处理】CDOJ1640 花自飘零水自流,一种相思,两处闲愁。
题意: 在n个点m条边的有向图上,从1出发的回路最多经过多少个不同的点 可以在一条边上逆行一次 题解: 在同一个强连通分量中,显然可以经过当中的每一个点 因此先将强连通分量缩点,点权为强连通分量的点数 ...
- [Luogu1119]采蘑菇
题目大意: 给你一个无向图,点i在时间t[i]之前是不存在的,有q组询问,问你时间为t时从x到y的最短路. 点的编号按出现的时间顺序给出,询问也按照时间顺序给出. 思路: Floyd. Floyd的本 ...
- SQL 中 HAVING 用法
现在 Student表有 如下数据 现需求如下: 查找每个老师的学生的平均年龄且平均年齿大于12 不使用 HAVING SELECT * FROM (SELECT TeacherID, AVG(Age ...
- ORACLE启动 切换实例命令
启动服务器的其他实例 export ORACLE_SID=数据库实例名 sqlplus /nolog conn /as sysdba select name from v$database; !lsn ...