-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
125 lines (109 loc) · 3.71 KB
/
main.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
#!/usr/bin/python
# coding=utf-8
import urllib
import urllib2
import cookielib
import re
import time
import random
from datetime import datetime
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
def post(url, data, cj):
req = urllib2.Request(url)
data = urllib.urlencode(data)
# enable cookie
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
response = opener.open(req, data)
# DEBUG AREA
if debug >= 1:
print 'POST_REQUEST:\n' + url
print 'POST_CONTENT:\n' + data
return response.read()
def browse(url, cj):
try:
req = urllib2.Request(url)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
response = opener.open(req)
if debug > 4:
print 'BROSWE_REQUEST:\n' + url
print 'BROSWE_RESPONSE:\n' + response.read()
return response.read()
except:
return ''
def downImg(url, name):
fr = urllib.urlopen(url)
stream = fr.read(-1)
fr.close()
fw = open('picture/' + name, 'w')
fw.write(stream)
fw.close()
def main(user_name, user_password):
du = 'http://m.douban.com'
# set Group ID
group_id = 'haixiuzu'
# set refresh_interval
refresh_interval = 1
# set form_email, and form_password
data = {'form_email': user_name,
'form_password': user_password, 'action': '/'}
# set group all_pages
all_pages = 10
# login
try:
post(du, data, cj)
except:
print 'FATAL'
exit(0)
times = 0
for page in range(1, all_pages):
# Get Groups
group_content = browse(
du + '/group/' + group_id + '/topics?start=' + str(page * 25), cj)
print page
replace_dict = {'\n': '', '\t': '', ' ': '', ' ': ''}
group_content = replace_all(group_content, replace_dict)
if debug >= 1:
print group_content
if times == 0:
group_title = re.findall(
'<h1class="group-name">(.*)<\/h1>', group_content)[0]
print '\n-----download "' + group_title + '"ing-----'
times += 1
items = re.findall(
'<ahref="\/group\/topic\/(\d+)\/"title="(.*?)"><.*?<divclass="info">(\d+)回应', group_content)
if not items.__len__() == 0:
for i in items:
item_title = i[1]
item_title = item_title.replace('/', '')
item_ID = i[0]
item_comm = i[2]
if int(item_comm) > 1:
print item_title + ' | ' + item_comm
item_url = du + '/group/topic/' + item_ID + '/'
item_content = browse(item_url, cj)
img_content = replace_all(item_content, replace_dict)
imgs = re.findall(
'<divclass="content_img"><imgsrc="(.*?)"/>', img_content)
if not imgs.__len__() == 0:
num = 0
for img_url in imgs:
num = num + 1
filename = str(item_title) + str(num) + '.jpg'
try:
downImg(img_url, filename)
except:
pass
time.sleep(refresh_interval * random.randint(2, 5))
if debug == 1:
time.sleep(refresh_interval)
else:
time.sleep(refresh_interval * random.randint(3, 7))
if __name__ == '__main__':
cj = cookielib.CookieJar()
debug = 0
user_name = raw_input('豆瓣 用户名\n')
user_password = raw_input('\n豆瓣 密码\n')
main(user_name, user_password)