-
Notifications
You must be signed in to change notification settings - Fork 0
/
latest.py
executable file
·87 lines (64 loc) · 1.97 KB
/
latest.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
#!/usr/bin/env python
#encoding=utf8
"""
get lastest smth job information
author: chenaway@qq.com
"""
import requests as r
import re
g = r.get
fa = re.findall
def remove_top(html):
''' remove top items in first page '''
return re.sub('<tr class="top">.*?</tr>', '', html)
def html_clean(html):
''' clean html tags and escaped chars '''
ret = re.sub('<br />', '\n', html)
ret = re.sub(' ', ' ', ret)
ret = re.sub('<[^<]*>', '', ret)
return ret
def local_page(page):
''' get local page '''
with open('%s.html' % page) as f:
return f.read()
def get_index_page(page):
''' get list of topics '''
# get html
url = 'http://www.newsmth.net/nForum/board/Career_Upgrade?ajax&p=%s' % page
html = g(url).content.decode('gbk').encode('utf8')
open('%s.html' % page, 'w+').write(html)
# remove top items
if page == 1:
html = remove_top(html)
return html
def get_index_title(html):
# get links and titles
pattern = u'<a href="/nForum/article/Career_Upgrade/(\d+)">([^<]*)</a>'
links = fa(pattern, html)
return links
def get_detail_page(tid):
''' get the detail page '''
url = 'http://www.newsmth.net/nForum/article/Career_Upgrade/%s?ajax' % tid
html = g(url).content.decode('gbk')
return html
def get_detail(html):
''' get detail of a topic '''
detail = re.findall(u'站内(.*?)来源', html)[0]
detail = html_clean(detail)
return detail.encode('utf8')
def get_detail_with_comment(html):
''' get detail of a topic with comment '''
detail_with_comment = re.findall(u'站内(.*)来源', html)[0]
detail_with_comment = html_clean(detail_with_comment)
return detail_with_comment.encode('utf8')
def main():
page = 1
html = get_index_page(page)
links = get_index_title(html)
for tid, title in links:
print '-'*30
print tid, title
html = get_detail_page(tid)
print get_detail(html)
if __name__ == '__main__':
main()