-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_movie_explore.py
60 lines (56 loc) · 2.03 KB
/
get_movie_explore.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
# coding: utf-8
import json
import requests
import urllib
from login import Client
import time
# 拉取豆瓣高分没看过的,评分大于sore分数的电影。一共收集x部电影
class GetMovieExplore:
def __init__(self):
self.login = Client('cookies')
self.sore = 8
self.x = 1000
self._session = self.login.return_session()
self.header = {
'Referer': 'https://movie.douban.com/explore',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0',
'Host': 'movie.douban.com',
'x-requested-with': "XMLHttpRequest"
}
self._session.headers.update(self.header)
def get_explore_json(self):
result = []
data = {
"type": "movie",
"tag": "豆瓣高分",
"sort": "recommend",
"watched": "on",
"page_limit": 20,
"page_start": 0
}
count = 0
while len(result) < self.x:
last_len = len(result)
data['page_start'] = data['page_limit'] * count
url_str = '/j/search_subjects?'
for key in data:
url_str += key + '=' + urllib.quote(str(data[key]))
count += 1
self.header['path'] = url_str
req = requests.Request('GET',
'http://movie.douban.com/j/search_subjects',
params=data,
headers=self.header)
prepped = self._session.prepare_request(req)
resp = self._session.send(prepped)
result.extend(resp.json()['subjects'])
time.sleep(2)
if last_len == len(result):
break
# todo: sort
# result = sorted(result.iteritems(), key=lambda d:d[1], reverse = True)
with open('high_mark_movie.json', 'w') as f:
f.write(json.dumps(result))
if __name__ == "__main__":
gme = GetMovieExplore()
gme.get_explore_json()