-
Notifications
You must be signed in to change notification settings - Fork 16
/
wallbase.py
86 lines (69 loc) · 2.22 KB
/
wallbase.py
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
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
#encoding: utf-8
#author: zengqiu
import urllib2
import urllib
from BeautifulSoup import BeautifulSoup
import re
import urlparse
import os
import socket
image_path = "/home/mini/wallbase"
def spider(url):
user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"
headers = {'User-Agent': user_agent}
request = urllib2.Request(url, headers = headers)
response = urllib2.urlopen(request)
soup = BeautifulSoup(response.read())
results = []
for thumb in soup.findAll("img", attrs={"class": re.compile("file"), "data-original": True}):
thumb_parent = thumb.findParent("a")
#print thumb_parent['href']
result = spider_image(thumb_parent['href'])
results.append(result)
return results
def spider_image(url):
user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"
headers = {'User-Agent': user_agent}
request = urllib2.Request(url, headers = headers)
response = urllib2.urlopen(request)
soup = BeautifulSoup(response.read())
result = {}
img = soup.findAll("img", attrs={"class": re.compile("wall")}, limit=1)
result['url'] = img[0]['src']
for size in soup.findAll("div", "l1"):
if size.parent.name == "a":
result['size'] = size.text
break
return result
def download(url, path):
filename = re.split('/', urlparse.urlparse(url).path)[-1]
filepath = os.path.join(path, filename)
if not os.path.isfile(filepath):
try:
urllib.urlretrieve(url, filepath)
except:
print filename + " is not exist"
def makedir(path):
if not os.path.exists(path):
os.makedirs(path)
def run():
page = 0
enable = True
while enable:
url = "http://wallbase.cc/toplist/%d" % page
results = spider(url)
if results:
for result in results:
subpath = result['size']
newpath = os.path.join(image_path, subpath)
makedir(newpath)
download(result['url'], newpath)
page += 32
else:
enable = False
def main():
print 'Please use it as ./wallbase'
run()
if __name__ == '__main__':
main()