-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
executable file
·77 lines (56 loc) · 1.71 KB
/
search.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
#!/usr/bin/env python
#encoding=utf8
"""
get lastest smth job information, search by keyword
author: chenaway@qq.com
"""
import requests as r
import re
g = r.get
fa = re.findall
# ---------------
def get_html(url):
return g(url).content.decode('gbk').encode('utf8')
def search(keyword):
''' search for keyword'''
pattern = 'http://www.newsmth.net/nForum/s/article?\
ajax&t1=%s&au=&b=Career_Upgrade'
url = pattern % keyword
html = get_html(url)
return get_index_title(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 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():
links = search('python')
for tid, title in links:
print '-'*30
print tid, title
html = get_detail_page(tid)
print get_detail(html)
if __name__ == '__main__':
main()