-
Notifications
You must be signed in to change notification settings - Fork 0
/
spider.py
133 lines (119 loc) · 4.13 KB
/
spider.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/python
#Filename:spider.py
import sys
import requests
import re
import sqlite3
import uuid
import urllib
import threading,Queue
import pic
#import makegif
global mutex
mutex = threading.Lock()
class Spider:
def __init__(self,ad):
self.ad = ad
d = sqlite3.connect('test.db')
db = d.cursor()
search = 'SELECT PATH FROM IMAGE WHERE LAST=1 AND PAGE=\''+ad+'\''
db.execute(search)
self.last = db.fetchone()
d.close()
self.queue = Queue.Queue()
for i in range(10):
Download(self.queue,i)
self.crawl()
def crawl(self):
url = 'http://zhan.renren.com/'+self.ad+'?from=pages'
#url = 'http://zhan.renren.com/woaigif?from=pages'
#url = 'http://zhan.renren.com/yaoxiaomen?from=pages'
#url = 'http://zhan.renren.com/wenzinvliumang?from=pages'
#url = 'http://zhan.renren.com/diaosi365?from=pages'
self.page = 0
while True:
try:
payload = {'page':self.page}
r = requests.get(url,params = payload)
result = self.catch(r.content)
if self.receive(result):
for i in range(10):
self.queue.put(None)
break
self.page = self.page + 1
print self.page
except Exception:
break;
def catch(self,html):
#str1 = r'\);"><img src="'
str2 = r'data-src="'
str3 = r'" />'
str4 = r'" alt'
_str = r'(.*)'
str5 = r'" />'
str6 = r'<img src="'
result = re.findall(str6+_str+str4,html)
#result = re.findall(str5+_str+str4,html)
result = result + re.findall(str2+_str+str3,html)
result = result + re.findall(str2+_str+str5,html)
return result
def receive(self,result):
if len(result)==0:
return True
for rel in result:
if self.last!=None and rel == self.last[0]:
return True
else:
if len(rel)<120:
name = str(uuid.uuid1())
url = rel.split('.')
name = name + '.' + url[-1]
work = (rel,name)
print name
self.queue.put(work)
mutex.acquire()
d = sqlite3.connect('test.db')
db = d.cursor()
if rel==result[0] and self.page == 0:
db.execute('INSERT INTO IMAGE VALUES(NULL,\''+name+'\',\''+rel+'\',\''+self.ad+'\',1,0)')
d.commit()
if self.last!=None:
db.execute('UPDATE IMAGE SET LAST=0 WHERE PATH=\''+self.last[0]+'\'')
d.commit()
else:
db.execute('INSERT INTO IMAGE VALUES(NULL,\''+name+'\',\''+rel+'\',\''+self.ad+'\',0,0)')
d.commit()
d.close()
mutex.release()
class Download(threading.Thread):
def __init__(self,Myqueue,threadname):
threading.Thread.__init__(self,name=threadname)
self.queue = Myqueue
self.start()
def run(self):
while True:
try:
work = self.queue.get()
if work == None:
break
url = work[0]
name = work[1]
urllib.urlretrieve(url,'timage/'+name)
#nlist = name.split('.')
#if not nlist[-1] == 'gif':
pt = pic.Pic('timage/'+name)
height = pt.resize()
mutex.acquire()
d = sqlite3.connect('test.db')
db = d.cursor()
db.execute('UPDATE IMAGE SET HEIGHT='+str(height)+' WHERE uuid=\''+name+'\'')
d.commit()
d.close()
mutex.release()
#else:
# makegif.main('image/'+name)
except Exception:
print Exception
def main():
Spider(sys.argv[1])
if __name__=='__main__':main()