Python实现抓取http://www.cssmoban.com/cssthemes站点的模版并下载

实现代码

  1. # -*- coding: utf-8 -*-
  2. import urlparse
  3. import urllib2
  4. import re
  5. import os
  6. import os.path
  7.  
  8. URL='http://www.cssmoban.com/cssthemes'
  9.  
  10. #全局超时设置
  11. urllib2.socket.setdefaulttimeout(500)
  12.  
  13. #依据url获取内容
  14. def getUrlContent(url):
  15. response = urllib2.urlopen(url)
  16. html = response.read();
  17. return html
  18.  
  19. #获取html中的a标签。且格式是<a target="_blank" href="/showcase/*">的
  20. def getAllUrl(html):
  21. return re.findall('<a[\\s]+href="/cssthemes/\d+\.shtml">.*?
  22.  
  23. \/a>',html)
  24.  
  25. #获取下载文件的标题
  26. def getDownTitle(html):
  27. return re.findall('\<h1>(.*?
  28.  
  29. )\</h1>',html)
  30.  
  31. #获取文件下载的url
  32. def getDownUrl(html):
  33. return re.findall('<a.*?class="button btn-down".*?
  34.  
  35. \/a>',html)
  36.  
  37. #获取下一页的url
  38. def getNextUrl(html):
  39. return re.findall('<a.*?
  40.  
  41. 下一页</a>',html)
  42.  
  43. #下载文件
  44. def download(title,url):
  45. result = urllib2.urlopen(url).read()
  46. if os.path.exists("template/")==False:
  47. os.makedirs("template/")
  48. newname=("template/"+title.decode('utf-8'))
  49. newname=newname+'.'+url[url.rfind('.')+1:len(url)]
  50. open(newname, "wb").write(result)
  51.  
  52. #记录日志
  53. def i(msg):
  54. fileobj=open('info.log','a')
  55. fileobj.write(msg+'\n')
  56. fileobj.close();
  57. print msg
  58. #记录错误日志
  59. def e(msg):
  60. fileobj=open('error.log','a')
  61. fileobj.write(msg+'\n')
  62. fileobj.close();
  63. print msg
  64. if __name__ == '__main__':
  65.  
  66. #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>')
  67.  
  68. html= getUrlContent(URL)
  69. i('開始下载:%s' %(URL))
  70. while True:
  71. lista= getAllUrl(html);
  72. #print lista;
  73. nextPage=getNextUrl(html)
  74. #print nextPage[0]
  75. nextUrl=''
  76. #i('下一页%s'%(nextPage))
  77.  
  78. if len(nextPage)<=0:
  79. e('地址:%s。未找到下一页,程序退出' %(nextPage))
  80. break;
  81.  
  82. nextUrl=nextPage[0]
  83. nextUrl=URL+'/'+nextUrl[nextUrl.index('href="')+6:nextUrl.index('" target')]
  84. #print nextPage
  85. for a in lista:
  86. downGotoUrl=''
  87. try:
  88. #print a.decode('utf-8')
  89. downGotoUrl=(URL+''+a[a.index('href="')+6:a.index('">')])
  90. downGotoUrl=downGotoUrl.replace(URL,'http://www.cssmoban.com')
  91. #print downGotoUrl
  92. downHtml=getUrlContent(downGotoUrl)
  93. #print downHtml
  94. downTitleList= getDownTitle(downHtml)
  95. downTitle=''
  96. if len(downTitleList)>0:
  97. downTitle=downTitleList[0]
  98. #print downTitle
  99. downUrlList= getDownUrl(downHtml)
  100. downUrl=''
  101. if len(downUrlList)>0:
  102. downUrl=downUrlList[0]
  103. downUrl= downUrl[downUrl.index('href="')+6:downUrl.index('" target')]
  104. #print downUrl
  105. i('開始下载:%s,文件名称:%s' %(downUrl,downTitle))
  106.  
  107. download(downTitle,downUrl)
  108. i('%s下载完毕。保存文件名称:%s' %(downUrl,downTitle))
  109. except Exception,e:
  110. e('地址:%s下载失败,失败信息:' %(downGotoUrl))
  111. e(str(e))
  112.  
  113. i('-----------------------------------------')
  114. i('运行下一页:%s' %(nextUrl))
  115. 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. 2018CCPC 中国大学生程序设计竞赛 网络赛

    链接 1.括号序列贪心/CF&51nod原题 [分析]: 贪心,每次到i的时候,假如你要在i里面要卖掉股票,获益是a[i], 肯定要在前面要么:1)把已经卖了的变成不买不卖,需要-a[j], ...

  2. mysql-错误备查

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

  3. Bzoj3197/洛谷3296 [SDOI2013]刺客信条assassin(树的重心+树Hash+树形DP+KM)

    题面 Bzoj 洛谷 题解 (除了代码均摘自喻队的博客,可是他退役了) 首先固定一棵树,枚举另一棵树,显然另一棵树只有与这棵树同构才有可能产生贡献 如果固定的树以重心为根,那么另一棵树最多就只有重心为 ...

  4. A - Chinese Girls' Amusement ZOJ - 2313(大数)

    You must have heard that the Chinese culture is quite different from that of Europe or Russia. So so ...

  5. [Contest20171006]Subsequence Count

    给定一个01串$S_{1\cdots n}$和$Q$个操作.操作有两种类型:1.将$[l,r]$区间的数取反(将其中的$0$变成$1$,$1$变成$0$).2.询问字符串$S$的子串$S_{l\cdo ...

  6. [CF340D]Bubble Sort Graph/[JZOJ3485]独立集

    题目大意: 给你一个序列,对序列中所有逆序对之间连一条边,问图中最大独立集为多大,有哪些点一定在最大独立集中. 思路: 在纸上画一下发现最大独立集一定是元序列的一个LIS,最大独立集必经点就是所有LI ...

  7. [转] FileSystemXmlApplicationContext、ClassPathXmlApplicationContext和XmlWebApplicationContext简介

    今天在用Spring时遇到一个问题,提示找不到applicationContext.xml文件.原来是在加载这个文件时调用的方法不太合适,所以造成了程序找不到项目下的xml配置文件. 我们常用的加载c ...

  8. mysql启动错误解决

    mysql 启动时,报错一般都不明显,因此我们需要配置错误日志 #vim /etc/my.cnf xxxxxxxxxx 1   1 #vim /etc/my.cnf 在[mysqld]下添加 log_ ...

  9. 内核创建的用户进程printf不能输出一问的研究

    转:http://www.360doc.com/content/09/0315/10/26398_2812414.shtml 一:前言上个星期同事无意间说起,在用核中创建的用户空间进程中,使用prin ...

  10. TSynAuthentication SESSION验证

    TSynAuthentication SESSION验证 服务端维护的SESSIONS,实质上是一个array of integer,保存的是客户端的SESSIONID. SESSIONID可以由客户 ...