Python downloading JavaScript Effects

Source: Internet
Author: User

Python downloading JavaScript Effects

This article will share with you the code for capturing the JS script special effect template of the lazy library by using Python crawlers. It is implemented using a third-party library gevent. If you need it, you can refer to it.

This is a simple Python script. It is mainly used to download JavaScript special effect templates from the lazy library. The gevent third-party library is used in the script and needs to be installed first.

?

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: The Python crawler crawls the JS Script Template of the lazy Image Library.

Author: admin

Create-Date: 2015-05-25

Version: 1.0

'''

 

HTTP_URL = 'HTTP: // www.lanw.uku.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 code template ')

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 ()

# Use BeautifulSoup for parsing

DecodeHtml = BeautifulSoup (content)

Try:

ATags = decodeHtml. find_all ('div ', {'class': 'LIST-pngjs'}) [0]. find_all ('A ')

Failed t IndexError, e:

Print e

ATags = None

# Obtain the link address and title

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 t Exception, e:

Print e. message

 

Def getpageurl (xUrl ):

# List page Loop

Return [xUrl % page for page in xrange (1, 49)]

 

If _ name _ = '_ main __':

Jobs = []

Pageurl = getpageurl ('HTTP: // www.lanw.uku.com/js/p?s.html ')

# Crawling all links

For I in pageurl:

For k in geturllist (I). items ():

Jobs. append (gevent. spawn (download, k ))

Gevent. joinall (jobs)

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.