-
Notifications
You must be signed in to change notification settings - Fork 281
/
stock_pawer.py
302 lines (245 loc) · 6.95 KB
/
stock_pawer.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#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
browser = webdriver.Chrome(executable_path='F:\chromedriver_win32\chromedriver.exe')
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
#获取热门用户列表
def get_id():
url = 'http://xueqiu.com/people/all'
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"}
req = urllib2.Request( url, headers = headers)
try:
content = urllib2.urlopen(req).read()
except:
return
soup = BeautifulSoup(content)
name = soup.find('ul',class_='tab_nav')
h = name.findAll('a')
f = open('id.txt', 'w')
people = {}
for item in h:
link = item.get('href')
if link.find('id') != -1:
url = 'http://xueqiu.com' + link
print url
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"}
req = urllib2.Request( url, headers = headers)
try:
content = urllib2.urlopen(req).read()
except:
return
soup = BeautifulSoup(content)
name = soup.find('ul',class_='people')
h = name.findAll('li')
for item in h:
p = item.findAll('input')
print p[0].get('value').encode('utf-8'), p[1].get('value').encode('utf-8')
if not people.has_key(p[0].get('value').encode('utf-8')):
people[p[0].get('value').encode('utf-8')] = 0
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
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 sh.row_values(rownum)
#while 1:
# line = ff.readline()
# if not line:
# break
#print line
# array = line[:-1].split('%')
#print array
# s = stock(array[0], array[1], array[2])
# vstock.append(s)
print len(vstock)
print repr(vstock[0]._name)
#print chardet.detect(vstock[0]._name)
#print vstock[0]._name.decode('utf-8')
#return
#while 1:
# score = ff.readline()
# if not score:
# break
# array = score[:-1].split(' ')
# d[array[0]] = int(array[1])
#ff.close()
#i = 1000000000
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
#id = 'backwasabi'
#url = "http://xueqiu.com/" + id
#start(url)
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)