无比强大!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)
# -*- 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)
原文地址:https://blog.csdn.net/wiker_yong/article/details/25844349
无比强大!Python抓取cssmoban网站的模版并下载的更多相关文章
- 无比强大!Python抓取cssmoban站点的模版并下载
Python实现抓取http://www.cssmoban.com/cssthemes站点的模版并下载 实现代码 # -*- coding: utf-8 -*- import urlparse imp ...
- 用python抓取求职网站信息
本次抓取的是智联招聘网站搜索“数据分析师”之后的信息. python版本: python3.5. 我用的主要package是 Beautifulsoup + Requests+csv 另外,我将招聘内 ...
- python爬取视频网站m3u8视频,下载.ts后缀文件,合并成整视频
最近发现一些网站,可以解析各大视频网站的vip.仔细想了想,这也算是爬虫呀,爬的是视频数据. 首先选取一个视频网站,我选的是 影视大全 ,然后选择上映不久的电影 “一出好戏” . 分析页面 我用的是c ...
- python抓取网站提示错误ssl.SSLCertVerificationError处理
python在抓取制定网站的错误提示:ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify ...
- Python多进程方式抓取基金网站内容的方法分析
因为进程也不是越多越好,我们计划分3个进程执行.意思就是 :把总共要抓取的28页分成三部分. 怎么分呢? # 初始range r = range(1,29) # 步长 step = 10 myList ...
- Python抓取视频内容
Python抓取视频内容 Python 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年.Python语法简洁而清晰,具 ...
- 使用 Python 抓取欧洲足球联赛数据
Web Scraping在大数据时代,一切都要用数据来说话,大数据处理的过程一般需要经过以下的几个步骤 数据的采集和获取 数据的清洗,抽取,变形和装载 数据的分析,探索和预测 ...
- python抓取性感尤物美女图
由于是只用标准库,装了python3运行本代码就能下载到多多的美女图... 写出代码前面部分的时候,我意识到自己的函数设计错了,强忍继续把代码写完. 测试发现速度一般,200K左右的下载速度,也没有很 ...
- python抓取网页例子
python抓取网页例子 最近在学习python,刚刚完成了一个网页抓取的例子,通过python抓取全世界所有的学校以及学院的数据,并存为xml文件.数据源是人人网. 因为刚学习python,写的代码 ...
随机推荐
- sharding-jdbc,轻量级数据库分库分表中间件
Sharding-JDBC是当当应用框架ddframe中,从关系型数据库模块dd-rdb中分离出来的数据库水平分片框架,实现透明化数据库分库分表访问.Sharding-JDBC是继dubbox和ela ...
- jquery获取焦点和失去焦点
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 路飞学城Python-Day9
[23.函数-高阶函数]变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称为高阶函数如果一个函数可以接收另一个函数,这个函数就称为高阶函数 def func ...
- perl脚本去除文件中重复数据
今天第一天写博客,写的不好请大家多多指教,废话不多说了,干货送上: ############################################################# #!/u ...
- linux下mysqldump简单命令导出数据库和表
进入mysql的bin目录执行: 导出单个表: mysqldump -uroot -ppassword --database dbname --tables users > /home/root ...
- 学习爬虫:《Python网络数据采集》中英文PDF+代码
适合爬虫入门的书籍<Python网络数据采集>,采用简洁强大的Python语言,介绍了网络数据采集,并为采集新式网络中的各种数据类型提供了全面的指导.第一部分重点介绍网络数据采集的基本原理 ...
- JMS消息
1.消息可分为3部分:消息头.属性和有效负载 消息头:用于标识消息.声明消息属性及提供路由信息的特殊字段组成. 消息的属性区包含了和该消息有关的附加元数据,这个元数据由应用程序开发者进行设置,或者由J ...
- 不要在.h文件中定义变量
今天在头文件.h中初始化了一个数组和函数,在编译的时候提示这个数组和函数重新定义了,检查后发现,犯了一个致命的错误,在头文件中定义变量... 以下引用别人的一篇说明,警示自己. C语言作为一种结构化的 ...
- 【CS Round 34】Max Or Subarray
[题目链接]:https://csacademy.com/contest/round-34/summary/ [题意] 让你找一个最短的连续子串; 使得这个子串里面所有数字or起来最大; [题解] 对 ...
- jeesite 简介
jeesite 简介 https://github.com/thinkgem/jeesite http://jeesite.com/