Python下載懶人圖庫JavaScript特效
更新時間:2015年05月28日 09:44:05 投稿:hebedich
本文給大家分享的是使用Python 爬蟲抓取懶人圖庫的JS腳本特效模板的代碼,使用了第三方庫gevent來實現(xiàn),有需要的小伙伴可以參考下。
這是一個簡單的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
# 獲取鏈接地址和標(biāo)題
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):
# 進行列表頁循環(huán)
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)
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
Pytorch 實現(xiàn)數(shù)據(jù)集自定義讀取
今天小編就為大家分享一篇Pytorch 實現(xiàn)數(shù)據(jù)集自定義讀取,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Flask?+?MySQL如何實現(xiàn)用戶注冊,登錄和登出的項目實踐
本文主要介紹了Flask?+?MySQL?如何實現(xiàn)用戶注冊,登錄和登出的項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
詳解Python中數(shù)據(jù)庫管理模塊shelve和dbm的應(yīng)用
作為常用的 python 自帶數(shù)據(jù)庫管理模塊,shelve 和 dbm 都是非常方便的對象持久化存儲和檢索工具,本文將從用法、優(yōu)勢以及不同點等方面進行介紹,希望對大家有所幫助2023-10-10
快速實現(xiàn)基于Python的微信聊天機器人示例代碼
本篇文章主要介紹了快速實現(xiàn)基于Python的微信聊天機器人示例代碼,基于itchat開發(fā),可以用它做一個微信聊天機器人,有興趣的可以了解一下。2017-03-03
python使用urllib2提交http post請求的方法
這篇文章主要介紹了python使用urllib2提交http post請求的方法,涉及Python使用urllib2模塊的相關(guān)技巧,需要的朋友可以參考下2015-05-05

