forked from garydai/stock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxueqiuPawer.py
280 lines (219 loc) · 6.87 KB
/
xueqiuPawer.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#coding: utf-8
from selenium import webdriver
import time
import os
import re
import urllib2
import sys
from bs4 import BeautifulSoup
import os
import logging
import time
from datetime import datetime
from datetime import timedelta
from datetime import date
import threading
import time
import json
import xlrd
import xlwt
from xlrd import open_workbook
from xlutils.copy import copy
#import nltk
import threading
import time
description_id = 1
#windows
#browser = webdriver.Chrome(executable_path='F:\chromedriver_win32\chromedriver.exe')
#mac
browser = webdriver.Chrome(executable_path='/Users/daitechang/Documents/stock/chromedriver')
def start(url, d, today, vstock):
# try:
global description_id
global browser
url = url
try:
browser.get(url)
t = browser.page_source
pn = re.compile(ur'(.*)"statuses":(.*?)}]', re.S)
match = pn.match(t)
if not match:
# browser.close()
# browser.quit()
return 0
result = match.group(2)
result = result + '}]'
decode = json.loads(result)
startDetect = time.time()
st = int(time.mktime(datetime.strptime(datetime.strftime(today, "%Y-%m-%d"), "%Y-%m-%d").timetuple()))
ed = int(time.mktime(datetime.strptime(datetime.strftime(today + timedelta(days = 1), "%Y-%m-%d"), "%Y-%m-%d").timetuple()))
st = str(st) + '000'
print st
ed = str(ed) + '000'
print ed
s_today = datetime.strftime(today, "%Y-%m-%d")
for i in range(len(vstock)):
for item in decode:
if item['mark'] == 1:
continue
#print item['created_at'], st, ed
#print item['description'].encode('utf-8'), vstock[i]._name
if str(item['created_at']) > st and str(item['created_at']) < ed:
if item['text'].encode('utf-8').find(vstock[i]._name) != -1:
print 2
ff = open('corpus/' + s_today + '_' + str(description_id) + '.txt', 'w')
ff.write(item['text'].encode('utf-8'))
ff.close()
description_id += 1
#print vstock[i]._name, item['description'].encode('utf-8')
if d.has_key(i):
d[i] = d[i] + 1
else:
d[i] = 1
elif str(item['created_at']) < st and i == len(vstock) -1:
#print 1
# browser.close()
# browser.quit()
#if i == len(vstock) -1:
return 0
#print array[0], array[1]
# print decode[0]['description'].encode('utf-8')
# browser.close()
# browser.quit()
return 1
except Exception , e:
print e
# browser.close()
# browser.quit()
return 0
import json
#获取热门用户列表
def get_id():
f = open('id.txt', 'w')
for i in range(25):
url = 'http://xueqiu.com/recommend/user/industry.json?detail=1&index=' + str(i)
#browser.get(url)
#t = browser.page_source
print url
# print t.encode('utf-8')
cookie = '''s=10ht15dh2y; xq_a_token=5e47e2777e3b08d99725fe0f9f78815eb1cb8374; xqat=5e47e2777e3b08d99725fe0f9f78815eb1cb8374; xq_r_token=c38fedb2680c6b923eb4c87f16ebf19f574c3eca; xq_is_login=1; u=6585534947; xq_token_expire=Sun%20Nov%2015%202015%2009%3A14%3A02%20GMT%2B0800%20(CST); bid=73fe343eeb79fd513ae47464f938acf9_ig040t46; snbim_minify=true; __utmt=1; __utma=1.2082135748.1445390046.1445497172.1445504051.8; __utmb=1.14.10.1445504051; __utmc=1; __utmz=1.1445390046.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); Hm_lvt_1db88642e346389874251b5a1eded6e3=1445390044; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1445506132'''
headers = {"User-Agent":"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6","Cookie":cookie }
req = urllib2.Request( url, headers = headers)
try:
content = urllib2.urlopen(req).read()
except Exception, e:
print e
#return
js = json.loads(content)
#print js
industries = js['industries']
#print industries
if industries:
for industry in industries:
for user in industry['users']:
print user['id'], user['screen_name'].encode('utf-8')
f.write((str(user['id'])) + ' ' + (user['screen_name']).encode('utf-8') + '\n')
#f.write(p[0].get('value').encode('utf-8') + ' ' + p[1].get('value').encode('utf-8') + '\n')
print 1
class stock:
_id = ''
_name = ''
_industry = ''
def __init__(self, id, name, industry):
self._id = id
self._name = name
self._industry = industry
#import chardet
def pawner(day, t2):
today = date.today()
delta = -1
os.mkdir('corpus')
while 1:
f = open('id.txt', 'r')
delta += 1
if delta >= t2:
break
yesterday1 = today - timedelta(days = day - delta)
yesterday = datetime.strftime(yesterday1, "%Y-%m-%d")
score_file = 'score' + yesterday + '.txt'
industry_file = 'industry' + yesterday + '.txt'
#ff = open('score' + yesterday + '.txt', 'r')
d = {}
print score_file
vstock = []
#ff = open('stock.txt', 'r')
wb = xlrd.open_workbook('stock.xls')
sh = wb.sheet_by_name('stock')
for rownum in range(sh.nrows):
if rownum < 2:
continue
s = stock(str(sh.cell(rownum, 0).value), sh.cell(rownum, 1).value.encode('utf-8'), sh.cell(rownum, 2).value.encode('utf-8'))
vstock.append(s)
print len(vstock)
print repr(vstock[0]._name)
while 1:
try:
line = f.readline()
# user = str(i)
if not line:
break
array = line[:-1].split(' ')
user = array[0]
print array[0], array[1]
#user = "1676206424"
page = 1
while 1:
url = "http://xueqiu.com/" + user + "?page=" + str(page)
ret = start(url, d, yesterday1, vstock)
if ret == 0:
#print i
break
page = page + 1
time.sleep(2)
except Exception , e:
print e
continue
#break
#i = i + 1
#if i >=9999999999:
# break
f.close()
ff = open(score_file, 'w')
industry_p = open(industry_file, 'w')
rb = open_workbook('stock.xls')
rs = rb.sheet_by_name('stock')
wb = copy(rb)
ws = wb.get_sheet(0)
ncol = rs.ncols
ws.write(1, ncol, yesterday)
industry_d = {}
t = sorted(d.items(), lambda x, y: cmp(x[1], y[1]), reverse=True)
for key in t:
print str(vstock[key[0]]._name) + '%' + str(vstock[key[0]]._industry) + '%'+ str(key[1]) + '\n'
ff.write(str(vstock[key[0]]._name) + '%' + str(vstock[key[0]]._industry) + '%'+ str(key[1]) + '\n')
if industry_d.has_key(vstock[key[0]]._industry):
industry_d[vstock[key[0]]._industry] += 1
else:
industry_d[vstock[key[0]]._industry] = 1
ws.write(key[0] + 2, ncol, key[1])
t = sorted(industry_d.items(), lambda x, y: cmp(x[1], y[1]), reverse=True)
for key in t:
print str(key[0]) + '%' + str(key[1]) + '\n'
industry_p.write(str(key[0]) + '%' + str(key[1]) + '\n')
print industry_d
wb.save('stock.xls')
browser.close()
browser.quit()
# timer = threading.Timer(7200, pawner)
# timer.start()
if __name__ == "__main__":
#nltk.download()
#negids = movie_reviews.fileids('neg')
#posids = movie_reviews.fileids('pos')
#print 1
## timer = threading.Timer(7200, pawner)
# timer.start()
t = int(sys.argv[1])
t2 = int(sys.argv[2])
#get_id()
pawner(t, t2)