Python下載懶人圖庫JavaScript特效

來源:互聯網
上載者:User

Python下載懶人圖庫JavaScript特效

   本文給大家分享的是使用Python 爬蟲抓取懶人圖庫的JS指令碼特效模板的代碼,使用了第三方庫gevent來實現,有需要的小夥伴可以參考下。

  這是一個簡單的Python指令碼,主要從懶人圖庫下載JavaScript特效模板,在指令碼中使用了gevent這個第三方庫,使用的時候需要先安裝。

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

#!/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)

  以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.