/
lagou.py
226 lines (190 loc) · 6.5 KB
/
lagou.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Built-in
import re
import json
import os
import sys
from datetime import datetime
#from pprint import pprint
from functools import wraps
# third
import requests
from bs4 import BeautifulSoup
import jieba
# modules
from auth import Logging
from auth import ValidationError
class Kind(object):
def __init__(self):
self.job_list = ['houduankaifa', 'yidongkaifa', 'qianduankaifa', 'ceshi', 'yunwei', 'DBA', 'gaoduanjishuzhiwei',
'yingjiankaifa2', 'qiyeruanjian']
self.job_list_prefix = 'http://www.lagou.com/zhaopin/'
def parse(self):
self.job_list_url = [self.job_list_prefix +
temp for temp in self.job_list]
for job_list in self.job_list_url:
pass
class User(object):
def __init__(self):
pass # 主要是如果用户登陆的话,可以取得想要查看的结果。该项工作的优先级可以往后放
class Job(object):
job_prefix = 'http://www.lagou.com/jobs/'
def __init__(self, url=None, position_id=None):
if url:
self.url = url
if not hasattr(self, 'url'):
self.url = job_prefix + '/' + str(position_id) + '.html'
def parse(self):
r = requests.get(self.url)
self.soup = BeautifulSoup(r.content, 'lxml')
def parse_handle(func):
@wraps(func)
def func_wrapper(self):
if not hasattr(self, 'soup'):
self.parse()
return func(self)
return func_wrapper
@parse_handle
def get_title(self):
soup = self.soup
try:
title = soup.find_all('h1')[1]['title']
except AttributeError as ex:
Logging.error(u'无法解析职位名称')
Logging.error(ex)
title = None
return title
@parse_handle
def get_company(self):
soup = self.soup
try:
company = re.sub(
r'\s+', '', soup.find('h2', class_='fl').get_text())
# 这里应该还有更好的方法来取得公司名称,不包括’未认证‘的内容
except AttributeError as ex:
Logging.error(u'无法解析公司名称')
Logging.error(ex)
company = None
return company
@parse_handle
def get_description(self):
soup = self.soup
try:
description = soup.find(
'dd', class_='job_request').find_all('span')
salary = description[0].string
location = description[1].string
experience = description[2].string
education = description[3].string
jtype = description[4].string
except IndexError, AttributeError:
Logging.error(u'无法解析职位信息')
Logging.error(ex)
raise ValueError('{} can\'t be resolved'.format(self.url))
return salary, location, experience, education, jtype
@parse_handle
def get_request(self):
soup = self.soup
keyword = []
request = soup.find('dd', class_='job_bt').find_all('p')
for req in request:
keyword.append(req.get_text())
result = set()
for kw in keyword:
res = jieba.cut(kw)
for r in res:
temp = r.encode('ascii', 'ignore')
if temp == '' or temp.isdigit() or temp == ',' or temp == '/':
continue
result.add(r)
# result.update([r.lower() for r in res if (r.encode('ascii','ignore')!='' and \
# not r.encode('ascii','ignore').isdigit())]) one-line not
# satisfy
return result
class Company(object):
# 这一部分是对公司进行评价
def __init__(self, url=None, slug=None):
company_prefix = 'http://www.lagou.com/gongsi/'
if slug:
self.url = company_prefix + str(slug) + '.html'
else:
self.url = url # No Validation 未进行验证
def parse(self):
r = requests.get(self.url)
self.soup = BeautifulSoup(r.content,"lxml")
def parse_handle(func):
@wraps(func)
def func_wrapper(self):
if not hasattr(self, 'soup'):
self.parse()
return func(self)
return func_wrapper
@parse_handle
def get_name(self):
soup=self.soup
try:
name = soup.find('div', class_='company_main').find(
'h1').get_text()
re.sub(r'\s+','',name)
except AttributeError:
raise ValidationError('u')
return name # 还有换行符没有处理好,sigh……
@parse_handle
def get_information(self):
soup=self.soup
try:
information=soup.find('div',id='basic_container').find_all('li')
prof=information[0].find('span').string
equity=information[1].find('span').string
scale=information[2].find('span').string
location=information[3].find('span').string
except AttributeError,IndexError:
raise Validation(u'some happened')
return prof,equity,scale,location
def main():
# url='http://www.lagou.com/zhaopin/houduankaifa'
url = 'http://www.lagou.com/jobs/positionAjax.json'
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36',
'Host': 'www.lagou.com',
'Origin': 'http://www.lagou.com',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'X-Requested-With': 'XMLHttpRequest'
}
params = {
'needAdditionalResult': 'False'
}
data = {
'first': 'False',
'pn': 320,
'kd': '后端开发'
}
# params=json.dumps(params)
try:
r = requests.post(url, headers=headers, params=params, data=data)
print r.url
for i in r.json()['content']['positionResult']['result']:
print i['positionId']
except TypeError, ValueError:
Logging.error(u'page out of range')
'''
with open('res.json','w+') as f:
f.write(r.content)
'''
if __name__ == '__main__':
# main()
'''
job = Job(url='http://www.lagou.com/jobs/1806035.html')
print job.get_title()
print job.get_company()
for i in job.get_description():
print i
print job.get_request()
'''
c = Company(slug=132600)
print c.get_name()
for i in c.get_information():
print i