无比强大!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生成的,在源代码里看不到,要怎么才能抓 ...
随机推荐
- STL容器 -- Stack
核心:后进后出, LIFO. 头文件: #include <stack> 常用的构造方法: stack<int> st1; //构造一个空的存放 int 型的栈 stack&l ...
- 01Trie【p4551(poj3764)】 最长异或路径
题目描述 给定一棵 n 个点的带权树,结点下标从 1 开始到 N .寻找树中找两个结点,求最长的异或路径. 异或路径指的是指两个结点之间唯一路径上的所有边权的异或. 个人: 首先强推一下01字典树(T ...
- 洛谷——P1680 奇怪的分组
P1680 奇怪的分组 题目背景 终于解出了dm同学的难题,dm同学同意帮v神联络.可dm同学有个习惯,就是联络同学的时候喜欢分组联络,而且分组的方式也很特别,要求第i组的的人数必须大于他指定的个数c ...
- webpack 打包过程及常用插件
前言 要理解webpack 首先明白三个概念:module,chunk,bundles,即输入,中间态,输出. chunk: This webpack-specific term is uesd in ...
- Sqli-labs介绍、下载、安装
SQLI和sqli-labs介绍 SQLI,sql injection,我们称之为sql注入.何为sql,英文:Structured Query Language,叫做结构化查询语言.常见的结构化数据 ...
- 更改Xamarin Android App名称
更改Xamarin Android App名称 Xamarin Android生成的App名称默认和项目名一致.修改该名称有两种方式. 第一种方式:右击Android项目,选择“属性”命令,然 ...
- AGC 020 B - Ice Rink Game
题面在这里! 倒着维护可以取的范围区间,判一下可不可能无解即可. #include<bits/stdc++.h> #define ll long long using namespace ...
- [UOJ407]Werewolf
题意:给一个无向图和一些询问$(S,E,L,R)$,问能否实现:从$S$出发,经过一些编号$\geq L$的节点后再通过编号$\leq R$的节点到达$E$ 先对每条边$(x,y)$以$\max(x, ...
- 【贪心】Codeforces Round #407 (Div. 2) A. Anastasia and pebbles
贪心地一个一个尽可能往口袋里放,容易发现和顺序无关. #include<cstdio> #include<iostream> using namespace std; type ...
- Django contenttypes 框架详解
一.什么是Django ContentTypes? Django ContentTypes是由Django框架提供的一个核心功能,它对当前项目中所有基于Django驱动的model提供了更高层次的抽象 ...