-
Notifications
You must be signed in to change notification settings - Fork 0
/
maoyan.py~
executable file
·58 lines (53 loc) · 1.46 KB
/
maoyan.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
#!/usr/bin/env python
# encoding: utf-8
import urllib2
import csv
import datetime
import sys
import time
import json
import os
reload(sys)
sys.setdefaultencoding("utf-8")
rowName = [u'showRate',u'movieId',u'subBoxOffice',u'releaseDay',u'boxRate'u'showZero',u'dailyBoxOffice',u'movieName']
def dataprocess(html):
content = json.loads(html)
fileName = '/home/lxz/python/maoyan/maoyan.csv'
if os.path.exists(fileName):
mode = 'ab'
else:
mode = 'wb'
csvfile = file(fileName,mode)
writer = csv.writer(csvfile)
if mode == 'wb':
writer.writerow([name.encode('utf8') for name in rowName])
for i in content["data"]:
info = [i["showRate"],i["movieId"],i["sumBoxOffice"],i["releaseDay"],i["boxRate"],i["showZero"],i["dailyBoxOffice"],i["movieName"]]
writer.writerow([str(d).encode('utf8') for d in info])
csvfile.close()
def main():
begin = datetime.date(2011,01,01)
end = datetime.date(2015,10,27)
timeset = []
timeremain = []
for i in range((end-begin).days + 1):
day = begin + datetime.timedelta(days = i)
timeset.append(day)
print timeset
while timeset:
for i in timeset:
try:
url = 'http://piaofang.maoyan.com/history/date/box.json?date='+str(i)+'&cnt=10'
html = urllib2.urlopen(url,timeout = 5).read()
#print html
start = clock()
dataprocess(html)
time.sleep(0.5)
print str(i) + str(start)
except Exception,e:
timeremain.append(i)
continue
timeset = timeremain
timeremain = []
if __name__ == '__main__':
main()