forked from DemonDamon/FinnewsHunter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler_cnstock.py
263 lines (241 loc) · 11.6 KB
/
crawler_cnstock.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
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 3 13:41:50 2018
@author: Damon Li
"""
import time, re, requests
from concurrent import futures
from bs4 import BeautifulSoup
from pymongo import MongoClient
import Text_Analysis.text_mining as tm
import gevent
from gevent import monkey,pool
monkey.patch_all()
class WebCrawlFromcnstock(object):
'''Crawl company news from 'http://company.cnstock.com/company/scp_gsxw/1',
'http://ggjd.cnstock.com/gglist/search/qmtbbdj/1',
'http://ggjd.cnstock.com/gglist/search/ggkx/1' website.
# Arguments:
totalPages: Number of pages set to be crawled.
Range: Divide total web pages into totalPages/Range parts
for multi-threading processing.
ThreadsNum: Number of threads needed to be start.
dbName: Name of database.
colName: Name of collection.
IP: Local IP address.
PORT: Port number corresponding to IP address.
'''
def __init__(self,**kwarg):
self.ThreadsNum = kwarg['ThreadsNum']
self.dbName = kwarg['dbName']
self.colName = kwarg['collectionName']
self.IP = kwarg['IP']
self.PORT = kwarg['PORT']
self.Prob = .5
self.realtimeNewsURL = []
self.tm = tm.TextMining(IP="localhost",PORT=27017)
def ConnDB(self):
'''Connect mongodb.
'''
Conn = MongoClient(self.IP, self.PORT)
db = Conn[self.dbName]
self._collection = db.get_collection(self.colName)
def countchn(self,string):
'''Count Chinese numbers and calculate the frequency of Chinese occurrence.
# Arguments:
string: Each part of crawled website analyzed by BeautifulSoup.
'''
pattern = re.compile(u'[\u1100-\uFFFDh]+?')
result = pattern.findall(string)
chnnum = len(result)
possible = chnnum/len(str(string))
return (chnnum, possible)
def getUrlInfo(self,url):
'''Analyze website and extract useful information.
'''
respond = requests.get(url)
respond.encoding = BeautifulSoup(respond.content, "lxml").original_encoding
bs = BeautifulSoup(respond.text, "lxml")
span_list = bs.find_all('span')
part = bs.find_all('p')
article = ''
date = ''
for span in span_list:
if 'class' in span.attrs and span['class'] == ['timer']:
date = span.text
break
for paragraph in part:
chnstatus = self.countchn(str(paragraph))
possible = chnstatus[1]
if possible > self.Prob:
article += str(paragraph)
while article.find('<') != -1 and article.find('>') != -1:
string = article[article.find('<'):article.find('>')+1]
article = article.replace(string,'')
while article.find('\u3000') != -1:
article = article.replace('\u3000','')
article = ' '.join(re.split(' +|\n+', article)).strip()
return date, article
def GenPagesLst(self,totalPages,Range,initPageID):
'''Generate page number list using Range parameter.
'''
PageLst = []
k = initPageID
while k+Range-1 <= totalPages:
PageLst.append((k,k+Range-1))
k += Range
if k+Range-1 < totalPages:
PageLst.append((k,totalPages))
return PageLst
def CrawlHistoryCompanyNews(self,startPage,endPage,url_Part_1):
'''Crawl historical company news
'''
self.ConnDB()
AddressLst = self.extractData(['Address'])[0]
if AddressLst == []:
urls = []
for pageId in range(startPage,endPage+1):
urls.append(url_Part_1 + str(pageId))
for url in urls:
print(url)
resp = requests.get(url)
resp.encoding = BeautifulSoup(resp.content, "lxml").original_encoding
bs = BeautifulSoup(resp.text, "lxml")
a_list = bs.find_all('a')
for a in a_list:
if 'href' in a.attrs and 'target' in a.attrs and 'title' in a.attrs \
and a['href'].find('http://company.cnstock.com/company/') != -1 \
and a.parent.find('span'):
date, article = self.getUrlInfo(a['href'])
while article == '' and self.Prob >= .1:
self.Prob -= .193
date, article = self.getUrlInfo(a['href'])
self.Prob =.5
if article != '':
data = {'Date' : date,
'Address' : a['href'],
'Title' : a['title'],
'Article' : article}
self._collection.insert_one(data)
else:
urls = []
for pageId in range(startPage,endPage+1):
urls.append(url_Part_1 + str(pageId))
for url in urls:
print(' <Re-Crawl url> ', url)
resp = requests.get(url)
resp.encoding = BeautifulSoup(resp.content, "lxml").original_encoding
bs = BeautifulSoup(resp.text, "lxml")
a_list = bs.find_all('a')
for a in a_list:
if 'href' in a.attrs and 'target' in a.attrs and 'title' in a.attrs \
and a['href'].find('http://company.cnstock.com/company/') != -1 \
and a.parent.find('span'):
if a['href'] not in AddressLst:
date, article = self.getUrlInfo(a['href'])
while article == '' and self.Prob >= .1:
self.Prob -= .1
date, article = self.getUrlInfo(a['href'])
self.Prob =.5
if article != '':
data = {'Date' : date,
'Address' : a['href'],
'Title' : a['title'],
'Article' : article}
self._collection.insert_one(data)
def CrawlRealtimeCompanyNews(self,url_part_lst):
'''Continue crawling company news from first website page
every once in a while and extract the useful information,
including summary, key words, released date, related stock
codes list and main body.
'''
doc_lst = []
self.ConnDB()
self._AddressLst = self.extractData(['Address'])[0]
for url_Part in url_part_lst:
url = url_Part + str(1)
resp = requests.get(url)
resp.encoding = BeautifulSoup(resp.content, "lxml").original_encoding
bs = BeautifulSoup(resp.text, "lxml")
a_list = bs.find_all('a')
if len(self.realtimeNewsURL) == 0:
for a in a_list:
if ('href' in a.attrs and 'target' in a.attrs and 'title' in a.attrs \
and a['href'].find('http://company.cnstock.com/company/') != -1 \
and a.parent.find('span')) or ('href' in a.attrs and 'target' in a.attrs \
and 'title' in a.attrs and a['href'].find('http://ggjd.cnstock.com/company/') != -1 \
and a.parent.find('span')):
if a['href'] not in self._AddressLst:
self.realtimeNewsURL.append(a['href'])
date, article = self.getUrlInfo(a['href'])
while article == '' and self.Prob >= .1:
self.Prob -= .1
date, article = self.getUrlInfo(a['href'])
self.Prob =.5
if article != '':
data = {'Date' : date,
'Address' : a['href'],
'Title' : a['title'],
'Article' : article}
self._collection.insert_one(data)
doc_lst.append(a['title'] + ' ' + article)
print(' [' + date + '] ' + a['title'])
else:
for a in a_list:
if ('href' in a.attrs and 'target' in a.attrs and 'title' in a.attrs \
and a['href'].find('http://company.cnstock.com/company/') != -1 \
and a.parent.find('span')) or ('href' in a.attrs and 'target' in a.attrs \
and 'title' in a.attrs and a['href'].find('http://ggjd.cnstock.com/company/') != -1 \
and a.parent.find('span')):
if a['href'] not in self.realtimeNewsURL and a['href'] not in self._AddressLst:
self.realtimeNewsURL.append(a['href'])
date, article = self.getUrlInfo(a['href'])
while article == '' and self.Prob >= .1:
self.Prob -= .1
date, article = self.getUrlInfo(a['href'])
self.Prob =.5
if article != '':
data = {'Date' : date,
'Address' : a['href'],
'Title' : a['title'],
'Article' : article}
self._collection.insert_one(data)
doc_lst.append(a['title'] + ' ' + article)
print(' [' + date + '] ' + a['title'])
return doc_lst
def extractData(self,tag_list):
'''Extract column data with tag in 'tag_list' to the list.
'''
data = []
for tag in tag_list:
exec(tag + " = self._collection.distinct('" + tag + "')")
exec("data.append(" + tag + ")")
return data
def coroutine_run(self,totalPages,Range,initPageID,**kwarg):
'''Coroutines running.
'''
jobs = []
page_ranges_lst = self.GenPagesLst(totalPages,Range,initPageID)
for page_range in page_ranges_lst:
jobs.append(gevent.spawn(self.CrawlHistoryCompanyNews,page_range[0],page_range[1],kwarg['url_Part_1']))
gevent.joinall(jobs)
def multi_threads_run(self,**kwarg):
'''Multi-threading running.
'''
page_ranges_lst = self.GenPagesLst()
print(' Using ' + str(self.ThreadsNum) + ' threads for collecting news ... ')
with futures.ThreadPoolExecutor(max_workers=self.ThreadsNum) as executor:
future_to_url = {executor.submit(self.CrawlHistoryCompanyNews,page_range[0],page_range[1]) : \
ind for ind, page_range in enumerate(page_ranges_lst)}
def classifyRealtimeStockNews(self):
'''Continue crawling and classifying news(articles/documents) every 60s.
'''
while True:
print(' * start crawling news from CNSTOCK ... ')
doc_list = self.CrawlRealtimeCompanyNews(['http://company.cnstock.com/company/scp_gsxw/',\
'http://ggjd.cnstock.com/gglist/search/qmtbbdj/',\
'http://ggjd.cnstock.com/gglist/search/ggkx/']) #
print(' * finish crawling ... ')
if len(doc_list) != 0:
self.tm.classifyRealtimeStockNews(doc_list)
time.sleep(60)