Python下载懒人图库JavaScript特效


Posted in Python onMay 28, 2015

这是一个简单的Python脚本,主要从懒人图库下载JavaScript特效模板,在脚本中使用了gevent这个第三方库,使用的时候需要先安装。

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import urllib,os,sys
import gevent,re
from gevent import monkey
from bs4 import BeautifulSoup
gevent.monkey.patch_socket()
 
'''
Description:Python 爬虫抓取懒人图库的JS脚本模板
Author:admin
Create-Date:2015-05-25
Version:1.0
'''
 
HTTP_URL = 'http://www.lanrentuku.com%s'
DOWNLOAD_URL = HTTP_URL[:-2] + '/js/d%szip'
reg=r'\d{1,}\.+'
 
def encode(text):
  return text.encode("utf8")
 
def createDirectory(curPath):
  myPath = os.path.join(getSubDirectory(), u'JS代码模板')
  if not os.path.exists(myPath):
    os.mkdir(myPath)
  return os.path.join(myPath, curPath)
 
def getSubDirectory():
  return os.getcwd()
 
def schedule(a, b, c): 
  per = 100.0 * a * b / c
  if per > 100 :
    per = 100
  sys.stdout.write('%.1f%%\r' % per)
  sys.stdout.flush()
 
def geturllist(url):
  url_list = {}
  html = urllib.urlopen(url)
  content = html.read()
  html.close()
  # 用BeautifulSoup解析
  decodeHtml = BeautifulSoup(content)
  try:
    aTags = decodeHtml.find_all('div', {'class':'list-pngjs'})[0].find_all('a')
  except IndexError, e:
    print e
    aTags = None
  # 获取链接地址和标题
  if aTags is not None:
    for a_tag in aTags:
      url_list[HTTP_URL % a_tag.get('href')] = a_tag.get_text()
  return url_list
  
def download(down_url):
  try:
    m=re.search(reg,down_url[0])
    name = DOWNLOAD_URL % m.group(0)
    urllib.urlretrieve(name,createDirectory(down_url[1] + name[-4:]),schedule)
  except Exception, e:
    print e.message
  
def getpageurl(xUrl):
  # 进行列表页循环
  return [xUrl % page for page in xrange(1,49)]
 
if __name__ == '__main__':
  jobs = []
  pageurl = getpageurl('http://www.lanrentuku.com/js/p%s.html')
  # 爬取所有链接
  for i in pageurl:
    for k in geturllist(i).items():
      jobs.append(gevent.spawn(download, k))
  gevent.joinall(jobs)

以上所述就是本文的全部内容了,希望大家能够喜欢。

Python 相关文章推荐
Python转换HTML到Text纯文本的方法
Jan 15 Python
Python命令启动Web服务器实例详解
Feb 23 Python
Python的IDEL增加清屏功能实例
Jun 19 Python
python中将一个全部为int的list 转化为str的list方法
Apr 09 Python
运行django项目指定IP和端口的方法
May 14 Python
Python实现ping指定IP的示例
Jun 04 Python
Python统计时间内的并发数代码实例
Dec 28 Python
Python3爬虫中Splash的知识总结
Jul 10 Python
Python常驻任务实现接收外界参数代码解析
Jul 21 Python
python unichr函数知识点总结
Dec 16 Python
Python 解决空列表.append() 输出为None的问题
May 23 Python
python+opencv实现视频抽帧示例代码
Jun 11 Python
Python实现给qq邮箱发送邮件的方法
May 28 #Python
Python import用法以及与from...import的区别
May 28 #Python
Python中使用不同编码读写txt文件详解
May 28 #Python
Python实现统计英文单词个数及字符串分割代码
May 28 #Python
python处理大数字的方法
May 27 #Python
python类继承用法实例分析
May 27 #Python
python显示生日是星期几的方法
May 27 #Python
You might like
PHP+MySQL实现的简单投票系统实例
2016/02/24 PHP
为Plesk PHP7启用Oracle OCI8扩展方法总结
2019/03/29 PHP
PHP实现简易用户登录系统
2020/07/10 PHP
javascript控制swfObject应用介绍
2012/11/29 Javascript
捕获键盘事件(且兼容各浏览器)
2013/07/03 Javascript
JSF中confirm弹出框的用法示例介绍
2014/01/07 Javascript
基于jquery实现百度新闻导航菜单滑动动画
2016/03/15 Javascript
详解vue-cli下ESlint 配置说明
2018/09/03 Javascript
如何为你的JS项目添加智能提示与类型检查详解
2019/03/12 Javascript
解决node终端下运行js文件不支持ES6语法
2020/04/04 Javascript
Vue使用screenfull实现全屏效果
2020/09/17 Javascript
[01:01]青春无憾,一战成名——DOTA2全国高校联赛开启
2018/02/25 DOTA
python anaconda 安装 环境变量 升级 以及特殊库安装的方法
2017/06/21 Python
Python简单计算文件MD5值的方法示例
2018/04/11 Python
python+openCV调用摄像头拍摄和处理图片的实现
2019/08/06 Python
python numpy之np.random的随机数函数使用介绍
2019/10/06 Python
Python使用selenium + headless chrome获取网页内容的方法示例
2019/10/16 Python
python字符串替换re.sub()实例解析
2020/02/09 Python
jupyter notebook 使用过程中python莫名崩溃的原因及解决方式
2020/04/10 Python
Python使用pycharm导入pymysql教程
2020/09/16 Python
如何通过python计算圆周率PI
2020/11/11 Python
python3实现简单飞机大战
2020/11/29 Python
DAWGS鞋官方网站:鞋,凉鞋,靴子
2016/10/04 全球购物
网站域名和主机:Domain.com
2019/04/01 全球购物
Blue Nile蓝色尼罗河香港官网:世界最大在线钻石珠宝销售商
2020/05/07 全球购物
垃圾回收的优点和原理。并考虑2种回收机制
2016/10/16 面试题
大学生物业管理求职信
2013/10/24 职场文书
最新茶叶店创业计划书
2014/01/14 职场文书
上班玩游戏检讨书
2014/02/07 职场文书
中秋节主持词
2014/04/02 职场文书
爱岗敬业演讲稿
2014/05/05 职场文书
大学优秀班集体申报材料
2014/05/23 职场文书
民政局个人整改措施
2014/09/24 职场文书
公务员年度考核评语
2014/12/31 职场文书
教师节祝酒词
2015/08/11 职场文书
Apache Pulsar结合Hudi构建Lakehouse方案分析
2022/03/31 Servers