Python下载懒人图库JavaScript特效

yipeiwu_com5年前Python基础

这是一个简单的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 实现某个功能每隔一段时间被执行一次的功能方法

本人在做项目的时候遇到一个问题: 某个函数需要在每个小时的 3 分钟时候被执行一次,我希望我 15:45 启动程序,过了18 分钟在 16:03 这个函数被执行一次,下一次过 60 分钟...

pymongo中聚合查询的使用方法

pymongo中聚合查询的使用方法

前言 在使用mongo数据库时,简单的查询基本上可以满足大多数的业务场景,但是试想一下,如果要统计某一荐在指定的数据中出现了多少次该怎么查询呢?笨的方法是使用find 将数据查询出来,再...

Python实现PS图像调整黑白效果示例

Python实现PS图像调整黑白效果示例

本文实例讲述了Python实现PS图像调整黑白效果。分享给大家供大家参考,具体如下: 这里用Python 实现 PS 里的图像调整–黑白,PS 里的黑白并不是简单粗暴的将图像转为灰度图,...

python插入排序算法的实现代码

1.算法:设有一组关键字{ K 1 , K 2 ,…, K n };排序开始就认为 K 1 是一个有序序列;让 K 2 插入上述表长为 1 的有序序列,使之成为一个表长为 2 的有序序列...

python处理文本文件并生成指定格式的文件

import os import sys import string #以指定模式打开指定文件,获取文件句柄 def getFileIns(filePath,model)...