forked from tanjiti/sec_profile
-
Notifications
You must be signed in to change notification settings - Fork 1
/
secwiki.py
273 lines (210 loc) · 6.62 KB
/
secwiki.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
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import logging
import glob
import re
import os
import codecs
import requests
from bs4 import BeautifulSoup
from mills import parse_url
from mills import SQLiteOper
from mills import path
from mills import get_weixin_info
from mills import get_github_info
from mills import get_twitter_info
from mills import d2sql
from mills import strip_n
from mills import get_title
def scrap_item(i=1):
"""
爬取单个页面
:return:
"""
url = "https://www.sec-wiki.com/weekly/{i}".format(i=i)
if not os.path.exists(path("data/secwiki")):
os.mkdir(path("data/secwiki"))
fname = path("data/secwiki/{i}_week.html".format(i=i))
logging.info("[SCRAP_PAGE]: %s" % url)
try:
r = requests.get(url)
if r.status_code == 200:
with codecs.open(fname, mode='wb') as fw:
fw.write(r.content)
return fname
except Exception as e:
logging.error("[SCRAP_REQUEST_FAILED]: %s %s" % (url, str(e)))
def scrap_all(start, end):
"""
爬取指定页面
:param start:
:param end:
:return:
"""
fnames = []
for i in range(start, end):
fname = scrap_item(i)
fnames.append(fname)
return fnames
def sort_fname(fnames):
"""
文件名排序
:return:
"""
nos = {}
for fname in fnames:
m = re.search(r'(\d+)_week\.html', fname)
if m:
nos[int(m.group(1))] = fname
return nos
def scrap_latest():
"""
爬取最新页面
:return:
"""
cur_no = -1
url = "https://www.sec-wiki.com/weekly"
try:
r = requests.get(url)
if r.status_code == 200:
html_content = r.content
soup = BeautifulSoup(html_content, "lxml")
weekly_string = soup.find("div", class_="issues").a["href"][8:]
cur_no = int(weekly_string)
except Exception as e:
logging.error("[REQUEST]: %s %s" % (url, str(e)))
nos = sort_fname(glob.glob("data/secwiki/*.html"))
if nos:
last_no = max(nos.keys())
else:
last_no = 0
if cur_no > 0:
if cur_no > last_no:
fnames = scrap_all(last_no + 1, cur_no + 1)
return fnames
def parse_item(html_hd, so=None, proxy=None):
"""
解析单个页面
:param page:
:return:
"""
soup = BeautifulSoup(html_hd, "lxml")
# find_day
# 2014/03/03-2014/03/09
day = soup.find("blockquote").text
p = re.compile(r'(\d{4})\/(\d{2})\/(\d{2})')
m = re.search(p, day)
if m:
ts = m.group(1) + m.group(2) + m.group(3)
else:
return
page = soup.find(id="content")
for div in page.find_all("div", class_='single'):
sts = div.stripped_strings
tag = sts.next()
if tag.find("[") != -1:
tag = tag[1:-1]
title = sts.next()
# url
for url in div.find_all("a"):
url = url["href"]
o, ext = parse_url(url)
domain = o.netloc
url_path = o.path
root_domain = ext.domain + "." + ext.suffix
title = strip_n(title)
domain_name = ""
try:
domain_name = get_title(domain)
except Exception as e:
logging.error("[get_domain_name]: %s %s" % (domain_name, str(e)))
if domain_name:
domain_name = re.sub('\x22', '', domain_name)
domain_name = re.sub('\x27', '', domain_name)
update_sql = "update {table} set domain_name='{title}' where domain='{domain}';".format(
table="secwiki_detail",
title=domain_name,
domain=domain
)
try:
so.execute(update_sql)
print(update_sql)
except Exception as e:
logging.error("[update_sql]: %s str(%s)" % (update_sql, str(e)))
sql = ""
if url.find("://twitter.com") != -1:
d = get_twitter_info(url, title, ts=ts, tag=tag, proxy=proxy)
if d:
sql = d2sql(d, table="twitter")
elif url.find("weixin.qq.com") != -1:
d = get_weixin_info(url, ts, tag)
if d:
sql = d2sql(d, table="weixin")
elif url.find("//github.com") != -1:
d = get_github_info(url, title, ts=ts, tag=tag)
if d:
sql = d2sql(d, table='github')
if sql:
try:
print(sql)
so.execute(sql)
except Exception as e:
logging.error("[sql]: %s %s" % (sql, str(e)))
result = (ts, tag, url, title, root_domain, domain, url_path)
yield result
def parse_all(fnames=None, renew=False, proxy=None):
"""
批量解析页面
:param fnames:
:param renew 是否重新解析所有文件
:return:
"""
so = SQLiteOper("data/scrap.db")
if renew:
fnames = []
fname_gen = glob.iglob(r'data/secwiki/*.html')
sql = 'delete from `secwiki_detail`'
for f in fname_gen:
fnames.append(f)
so.execute(sql)
if fnames is None:
print("no new secwiki")
return
nos = sort_fname(fnames)
# sqlite handler
sql = """insert into `secwiki_detail`(`ts`,`tag`,`url`,`title`,`root_domain`,`domain`,`path`)
values(?,?,?,?,?,?,?);"""
# file handler
result_fname = path("data/secwiki_{start}_{end}.txt".format(
start=nos.keys()[0],
end=nos.keys()[-1]
))
if not renew and os.path.isfile(result_fname) and os.path.getsize(result_fname) > 0:
return
result_fh = codecs.open(result_fname, mode='wb')
for k in nos.keys():
fname = nos[k]
with open(fname, mode='r') as html_hd:
results_list = {}
for content in parse_item(html_hd, so=so, proxy=proxy):
if content:
k = content[0] + content[2]
results_list[k] = content
line = "\t".join(content)
print(line)
result_fh.write("{line}{linesep}".format(line=line, linesep=os.linesep))
so.executemany(sql, operate_list=results_list.values())
result_fh.close()
def main(renew=False):
"""
:param renew: 全量更新或增量更新
:return:
"""
filenames = scrap_latest()
parse_all(filenames, renew=renew)
if __name__ == "__main__":
"""
"""
main(renew=False)