-
Notifications
You must be signed in to change notification settings - Fork 0
/
maoyan_1.py
executable file
·64 lines (59 loc) · 1.84 KB
/
maoyan_1.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
__author__ = '31351'
#!/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")
def dataprocess(html):
content = json.loads(html)
fileName = r'E:\maoyan-master\maoyan_1.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 datetrans(str):
return datetime.date(int(str.split("-")[0]),int(str.split("-")[1]),int(str.split("-")[2]))
def date(begin, end):
begin = datetrans(begin)
end = datetrans(end)
timeset = []
for i in range((end-begin).days + 1):
day = begin+datetime.timedelta(days = i)
timeset.append(day)
return timeset
def main(timeset):
print timeset
timeremain = []
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
dataprocess(html)
time.sleep(0.5)
print i
except Exception,e:
timeremain.append(i)
continue
timeset = timeremain
timeremain = []
return 0
str1 = raw_input("begin date:")
str2 = raw_input("end date")
timeset = date(datetrans(str1),datetrans(str2))
main(timeset)