-
Notifications
You must be signed in to change notification settings - Fork 0
/
gter_bs_ex.py
283 lines (254 loc) · 10.9 KB
/
gter_bs_ex.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
# -*- coding: utf-8 -*-
""""""
import re
from datetime import datetime
from requests import Session
from bs4 import BeautifulSoup
from torndb import Connection
# from concurrent.futures import ThreadPoolExecutor
# from pyspider.libs.base_handler import *
# _thread_pool = ThreadPoolExecutor(8)
# db = Connection(host='', database='',
# user='', password='')
fetcher = Session()
auris = [
('http://bbs.gter.net/forum-49-1.html', '美国'),
('http://bbs.gter.net/forum-565-1.html', '加拿大'),
('http://bbs.gter.net/forum-486-1.html', '英国'),
('http://bbs.gter.net/forum-539-1.html', '欧洲诸国'),
('http://bbs.gter.net/forum-811-1.html', '港澳台'),
('http://bbs.gter.net/forum-812-1.html', '新加坡'),
('http://bbs.gter.net/forum-484-1.html', '日韩'),
('http://bbs.gter.net/forum-128-1.html', '澳洲新西兰')
]
def fetch_index():
""""""
r = fetcher.get(url='http://bbs.gter.net/forum.php?gid=454')
if 200 != r.status_code:
return
page = BeautifulSoup(r.content, 'html.parser')
_uris = page.select('table[class="fl_tb"] tr h2 a')
return [_u.get('href') for _u in _uris]
def fetch_srouce(url=''):
""""""
next = url
while next:
print(next)
r = fetcher.get(url=next)
if 200 != r.status_code:
return
page = BeautifulSoup(r.content, 'html.parser')
_duris = page.select('tbody[id^="normalthread"] .xst')
# print(_duris)
uris = [_du.get('href') for _du in _duris]
# print(uris)
for u in uris:
try:
fetch_detail(u)
except:
with open('fetch_fail.log', 'a') as fd:
fd.write(u)
fd.write('\n')
_next = page.select_one('.nxt')
if _next:
next = _next.get('href')
else:
next = None
return
def fetch_detail(url=''):
""""""
r = fetcher.get(url=url)
if 200 != r.status_code:
return
page = BeautifulSoup(r.content, 'html.parser')
_offers = page.select('table[summary^="offer"]')
if _offers:
# print(_offers)
offer_list = list()
for _o in _offers:
_offer = dict()
for tr in _o.select('tbody tr'):
try:
if '申请学校' in tr.get_text():
_coll = tr.get_text().split(':')
_offer['coll'] = _coll[1].strip('\n').strip()
elif '学位' in tr.get_text():
_degree = tr.get_text().split(':')
_offer['degree'] = _degree[1].strip('\n').strip()
elif '专业' in tr.get_text():
_corse = tr.get_text().split(':')
_offer['corse'] = _corse[1].strip('\n').strip()
elif '申请结果' in tr.get_text():
_result = tr.get_text().split(':')
_offer['result'] = _result[1].strip('\n').strip()
elif '入学年份' in tr.get_text():
_year = tr.get_text().split(':')
_offer['year'] = _year[1].strip('\n').strip()
# elif '入学学期' in tr.get_text():
# _xq = tr.get_text().split(':')
# print(_xq[1].strip('\n').strip())
elif '通知时间' in tr.get_text():
_time = tr.get_text().split(':')
_offer['time'] = _time[1].strip('\n').strip()
else:
continue
except:
continue
offer_list.append(_offer)
print(offer_list)
print('\n')
user = dict()
_profile = page.select_one('table[summary="个人情况"]')
for pr in _profile.select('tbody tr'):
try:
if 'TOEFL' in pr.get_text():
p = re.search(r'Overall: (\d+),', pr.get_text())
if p:
user['toefl'] = p.group(1)
else:
user['toefl'] = p.get_text()
# _toefl = pr.get_text().split(':')
# print(_toefl[2].strip('\n').strip()) GMAT
elif 'GRE' in pr.get_text():
p = re.search(r'Overall: (\d+),', pr.get_text())
if p:
user['gre'] = p.group(1)
else:
user['gre'] = p.get_text()
elif 'GMAT' in pr.get_text():
p = re.search(r'Overall: (\d+),', pr.get_text())
if p:
user['gmat'] = p.group(1)
else:
user['gmat'] = p.get_text()
elif 'GPA' in pr.get_text():
p = re.search(r'Overall: (\d+),', pr.get_text())
if p:
user['gpa'] = p.group(1)
else:
user['gpa'] = p.get_text()
elif 'IELTS' in pr.get_text():
p = re.search(r'Overall: (\d+),', pr.get_text())
if p:
user['ielts'] = p.group(1)
else:
user['ielts'] = p.get_text()
# _gre = pr.get_text().split(':')
# print(_gre[2].strip('\n').strip())
elif '本科学校档次' in pr.get_text():
_level = pr.get_text().split(':')
user['level'] = _level[1].strip('\n').strip()
elif '成绩' in pr.get_text():
# _sorce = pr.get_text().split(':')
# print(_sorce[1].strip('\n').strip())
user['sorce'] = pr.get_text().strip()
elif '专业' in pr.get_text():
_sorce = pr.get_text().split(':')
user['zy'] = _sorce[1].strip('\n').strip()
# user['sorce'] = pr.get_text().strip()
else:
continue
except:
continue
# name
_name = page.select_one('.authi .xw1')
user['name'] = _name.get_text()
print(user)
print('-' * 8)
try:
save_result(user, offer_list)
except:
pass
# print(_profile)
return
def save_result(user=None, offers=None):
""""""
_now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
ssql = "INSERT INTO `xiongyang`.`t_cs_students_src` (`f_site_id`, `f_country_id`, `f_level_id`, `f_surname`, `tmp_ins_cname`, `stu_cos_name`, `stu_gpa`, `stu_gmat_gre`, `stu_toefl`, `stu_ielts`, `f_features`, `updatetime`) VALUES ('26', '1', '4', %s, %s, %s, %s, %s, %s, %s, %s, %s)"
if user.get('gre'):
_gmat_gre = user['gre']
elif user.get('gmat'):
_gmat_gre = user['gmat']
else:
_gmat_gre = ''
_result = 0
try:
_result = db.execute(ssql, user.get('name', ''), user.get('level', ''), user.get('zy', ''),
user.get('gpa', ''), _gmat_gre, user.get('toefl', ''), user.get('ielts', ''),
user.get('sorce', ''), _now)
except:
_result = db.get("SELECT id FROM `xiongyang`.`t_cs_students_src` WHERE f_site_id = 26 AND f_surname = '{}'".format(user.get('name', '')))
print('user > ', _result)
if offers:
for o in offers:
try:
osql = "INSERT INTO `xiongyang`.`t_cs_cases_src` (`f_site_id`, `f_level_id`, `f_student_id`, `f_app_type`, `app_ins_name`, `app_cos_name`, `tmp_admission`, `f_app_date`, `f_admitted_date`, `updatetime`) VALUES ('26', '4', %s, %s, %s, %s, %s, %s, %s, %s)"
_year = o.get('year')
if len(_year) == 4:
year = _year + '/01/01'
else:
year = '2016/01/01'
_or = db.execute(osql, _result, o.get('degree', ''), o.get('coll', ''), o.get('corse', ''),
o.get('result', ''), year, o.get('time', ''), _now)
print('case - ', _or)
except:
continue
def main():
uris = fetch_index()
for u in uris:
print(u)
fetch_srouce(u)
# class Handler(BaseHandler):
# crawl_config = {
# }
# @every(minutes=24 * 60)
# def on_start(self):
# self.crawl('http://bbs.gter.net/forum.php?gid=454', callback=self.index_page)
# @config(age=10 * 24 * 60 * 60)
# def index_page(self, response):
# for each in response.doc('table[class="fl_tb"] tr h2 a').items():
# # print(each.attr.href)
# self.crawl(each.attr.href, callback=self.forum_page)
# def forum_page(self, response):
# """"""
# for each in response.doc('tbody[id^="normalthread"] .new .xst').items():
# self.crawl(each.attr.href, callback=self.detail_page)
# _n = response.doc('.nxt')
# if _n:
# print(_n.attr.href)
# self.crawl(_n.attr.href, callback=self.forum_page)
# def detail_page(self, response):
# # for tr in response.doc('table[summary^="offer"] tr').items():
# # print(tr)
# # 'table[class="cgtl"]'
# for t in response.doc('table[summary^="offer"]').items():
# print(t)
# return {
# "url": response.url,
# "title": response.doc('title').text(),
# }
# _headers = {
# 'Host': 'www.gter.net',
# 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0',
# 'Accept': 'application/json, text/javascript, */*; q=0.01',
# 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
# 'X-Requested-With': 'XMLHttpRequest',
# 'Referer': 'http://www.gter.net/offer/index.html',
# 'Cookie': '4B5x_c0ae_saltkey=4UaVRHo7; _miucms_session=8cdebcd675b9f4050a90fa773cadc925; 4B5x_c0ae_lastvisit=1471849474; 4B5x_c0ae_sid=mfT3am; 4B5x_c0ae_lastact=1471853224%09home.php%09spacecp; Hm_lvt_4bd66cbe45a640b607fe46c48f658746=1471853078; Hm_lpvt_4bd66cbe45a640b607fe46c48f658746=1471853328; VMhOIbxho_member=33b7e647df19901f49163fc88c6a8b1d; 4B5x_c0ae_miucms_uid=3745765; 4B5x_c0ae_ulastactivity=d76ffYG2h59JRlgOM8BfaLjJMA3DNzNSNEcCeuyHXqLyB3%2FtIDb%2B; VMhOIbxho_lastactivity=1471853308'
# }
# def test_gter():
# _url = 'http://www.gter.net/offer.json?_api_json=61%3A19ce180fb252f15ea5bb1721b01469f5&kw=&page=&page_num=24'
# r = fetcher.post(url=_url, headers=_headers)
# if 200 != r.status_code:
# return
# import json
# content = json.loads(r.content)
# data = content.get('data')
# for row in data:
# print(row)
if __name__ == '__main__':
test_gter()
# main()
# db.execute("DELETE FROM xiongyang.t_cs_students_src where f_site_id = 26")
# db.execute("DELETE FROM xiongyang.t_cs_cases_src where f_site_id = 26")
# fetch_srouce('http://bbs.gter.net/forum-1010-3.html')