无比强大!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,写的代码 ...
随机推荐
- 为什么同样的数据,俩人生成的obj和bin文件不一样
http://bbs.csdn.net/topics/270055083 编译器编译的时候可能有些东西依赖时间,或许是优化的原因,如果可以,换个编译器试试,或许两次编译的时候,强制把系统时间调成一个看 ...
- 【原创】Spring连接、事务代码分析
1.JdbcTemplate 当不使用事务时,jdbcTemplate的模板类,通过 Connection con = DataSourceUtils.getConnection(ge ...
- @RequestMapping[转]
转自 http://www.cnblogs.com/qq78292959/p/3760560.html#undefined 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST. ...
- DotNetCore.1.0.1-VS2015Tools.Preview2.0.3 相关问题及解决办法
本月16号,MS发布了 .NET Core 1.1.作为一个用贯MS产品的小盆友,我第一时间就把相关的安装包下载下来了,然后果断安装(入坑). 我猜你来看这篇博客可能遇到了和我一样的问题. 问题0:正 ...
- 大数相乘(牛客网ac通过)
2019-05-172019-05-17 大数相乘基本思想: 相乘相加,只不过大于10先不进位到计算完后统一进位 #include <iostream> #include <stri ...
- VB学习笔记(一)VB操作字符串
在vb中 dim a# 定义a变量为双精度型变量~ #是类型符 % 整型 & 长整型 !单精度 $ 字符型 VB中strconv 的作用 StrConv("要转换的字符串" ...
- NodeJS学习笔记 (2)文件系统操作-fs(ok)
原文:https://github.com/chyingp/nodejs-learning-guide/blob/master/%E6%A8%A1%E5%9D%97/fs.md#%E9%80%9A%E ...
- BZOJ 2565 最长双回文串(manacher)
565: 最长双回文串 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3343 Solved: 1692[Submit][Status][Discu ...
- argparse模块入门介绍——基于python3.7
转载:https://blog.csdn.net/weixin_41796207/article/details/80846406 首先说明,本人是想要学习如何使用argparse模块,打造命令行程序 ...
- HDU——T 2444 The Accomodation of Students
http://acm.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Memory Limi ...