昨天一个朋友说最近想换工作。想让我帮看下Boss现在的招聘情况如何。正好想到上个月写了个开源爬虫框架kcrawler
,最后添加了一个Boss
类支持,可以实现快速根据关键词查询不同岗位,不同行业的的招聘情况。有现成可用的库,帮助朋友也是举手之劳。
1. 安装 kcrawler
是开源的。开发同学可以选择clone
源码。但是如果想省事,就直接pip install
,然后导入项目使用即可。
2. 使用 提供两个类。
Crawler: 简单配置爬取任意网站
Boss: Boss专用(本文推荐)
2.1 爬虫基类 Crawler kcrawler
提供了一个爬虫基类 Crawler
, 它封装了一个通用型爬虫的基础功能。通过传入配置字典来实例化一个网站的crawler
对象,然后调用对象的crawl
方法,即可实现指定目标数据的爬取。支持html,json,图像的爬取。以下为Boss
的配置示例。
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 from kcrawler.core.Crawler import Crawlerdef queryjobpage_url (x) : p = list() if x[0 ]: p.append('i' + str(x[0 ])) if x[1 ]: p.append('c' + str(x[1 ])) if x[2 ]: p.append('p' + str(x[2 ])) return 'https://www.zhipin.com/{}/' .format('-' .join(p)) config = { 'targets' : { 'city' : { 'url' : 'https://www.zhipin.com/wapi/zpCommon/data/city.json' , 'method' : 'get' , 'type' : 'json' }, 'position' : { 'url' : 'https://www.zhipin.com/wapi/zpCommon/data/position.json' , 'method' : 'get' , 'type' : 'json' }, 'industry' : { 'url' : 'https://www.zhipin.com/wapi/zpCommon/data/oldindustry.json' , 'method' : 'get' , 'type' : 'json' }, 'conditions' : { 'url' : 'https://www.zhipin.com/wapi/zpgeek/recommend/conditions.json' , 'method' : 'get' , 'type' : 'json' }, 'job' : { 'url' : 'https://www.zhipin.com/wapi/zpgeek/recommend/job/list.json' , 'method' : 'get' , 'type' : 'json' }, 'queryjob' : { 'url' : 'https://www.zhipin.com/job_detail/' , 'method' : 'get' , 'type' : 'html' }, 'queryjobpage' : { 'url' : queryjobpage_url, 'method' : 'get' , 'type' : 'html' }, 'jobcard' : { 'url' : 'https://www.zhipin.com/wapi/zpgeek/view/job/card.json' , 'method' : 'get' , 'type' : 'json' } } } crawler = Crawler(config)
现在完成了爬虫对象的创建。有过爬虫经验的同学可能已经注意到了,怎么没有headers
?事实上,有两种方法传入headers
。一种是直接在配置字典config
增加headers
键值。例如:
在浏览器里整个复制Request Headers
,以文档字符串的形式赋值给变量headers
。
1 2 3 4 5 6 7 8 9 10 headers = ''' :authority: www.zhipin.com :method: GET :path: / .... cookie: xxx .... user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36 ''' config = {'headers' : headers, 'targets' : { ... }}
第二种推荐的方式是在当前目前下新建一个文件名为headers
的文件。以同样的方法在浏览器复制整个headers
字符串覆盖这个文件,直接覆盖,不用加文档字符变量。当config
字典没有提供headers
字段时,Crawler
会自动从headers
文件读取headers
字符串。
完成以上步骤,即可调用crawler.crawl(target)
方法爬取数据了。其中target
是配置字典config
中targets
定义的键。
1 data = crawler.crawl('job' )
2.2 Boss 类 使用Crawler
爬取的数据是网站的原始数据的,虽然已经转换成字典或者列表,但是要进一步得到感兴趣的字段,还需要自己提取。但是kcrawler
已经提供了一个Boss
类可以直接使用。
当然还是建议你先按照前文的方法先在浏览器中复制Request Headers
,保存到当前目录下的headers
文件中。然后就可以愉快地使用Boss
类实例化一个对象,爬取感兴趣的数据。
1 2 3 from kcrawler import Bossboss = Boss()
看到如下输出,表示成功读取到headers
文件的内容。
1 2 read heades from file. parse headers
2.2.1 招聘城市
返回:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [{'id': 101010000, 'name': '北京', 'pinyin': None, 'firstChar': 'b', 'rank': 1, 'children': [{'id': 101010100, 'name': '北京', 'pinyin': 'beijing', 'firstChar': 'b', 'rank': 1}]}, {'id': 101020000, 'name': '上海', ... ]
2.2.2 热门城市 1 hotcities = boss.hotcity()
返回:
1 2 3 4 5 6 7 [{'id': 100010000, 'name': '全国'}, {'id': 101010100, 'name': '北京'}, {'id': 101020100, 'name': '上海'}, {'id': 101280100, 'name': '广州'}, {'id': 101280600, 'name': '深圳'}, {'id': 101210100, 'name': '杭州'}, ...]
2.2.3 当前登陆用户所在城市 1 user_city = boss.userCity()
返回:
2.2.4 行业 1 industries = boss.industry()
返回:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [{'id': 100001, 'name': '电子商务'}, {'id': 100002, 'name': '游戏'}, {'id': 100003, 'name': '媒体'}, {'id': 100004, 'name': '广告营销'}, {'id': 100005, 'name': '数据服务'}, {'id': 100006, 'name': '医疗健康'}, {'id': 100007, 'name': '生活服务'}, {'id': 100008, 'name': 'O2O'}, {'id': 100009, 'name': '旅游'}, {'id': 100010, 'name': '分类信息'}, {'id': 100011, 'name': '音乐/视频/阅读'}, {'id': 100012, 'name': '在线教育'}, {'id': 100013, 'name': '社交网络'}, ...]
2.2.5 当前登陆用户期望岗位(三个)
返回:
1 2 3 [{'id': xxx, 'positionName': 'xxx'}, {'id': xxx, 'positionName': 'xxx'}, {'id': xxx, 'positionName': 'xxx'}]
2.4.6 给当前登陆用户推荐的岗位列表 boss.job(i, page)
方法需要提供两个参数,第一个是boss.expect()
返回列表的索引,例如0代表第一个期望岗位的推荐岗位;第二个参数page
用于指定数据分页,例如1是第一页。
返回:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [{'id': 'be8bfdcdf7e99df90XV-0t-8FVo~', 'jobName': '深度学习平台经理/技术中级', 'salaryDesc': '30-50K', 'jobLabels': ['深圳 福田区 购物公园', '5-10年', '本科'], 'brandName': '中国平安', 'brandIndustry': '互联网金融', 'lid': '411f6b88-8a83-437a-aa5f-5de0fc4da2b7.190-GroupC,194-GroupB.1'}, {'id': 'f649c225a1b9038f0nZ609W5E1o~', 'jobName': '推荐系统评测工程师', 'salaryDesc': '20-35K', 'jobLabels': ['深圳 南山区 科技园', '1-3年', '本科'], 'brandName': '腾讯', 'brandIndustry': '互联网', {'id': '94cfec046b98b9671H150tS8E1M~', 'jobName': '用户数据挖掘工程师', 'salaryDesc': '20-40K·15薪', 'jobLabels': ['深圳 南山区 南山中心', '经验不限', '本科'], 'brandName': '今日头条', 'brandIndustry': '移动互联网', 'lid': '411f6b88-8a83-437a-aa5f-5de0fc4da2b7.190-GroupC,194-GroupB.4'}, ...]
2.4.7 通过关键词搜索岗位 city
, industry
, position
是上文已经爬取的数据对应ID字段;query
不用说就是查询关键词;这里之所以使用两个不同的方法,因为Boss
搜索岗位第一页和第二页及之后的页面的URL组成结构不同,前者固定不变,后者是变化的。这也是为什么前文传入配置字典的URL使用了函数形式。
因此,如果你只对查询第一页数据感兴趣就使用queryjob
方法;如果你想继续查看第二页及以后的数据,就必须使用queryjobpage
方法。当然,事实上queryjobpage
的方法也可以爬第一页的数据,但是建议分开使用,这样较不容易触发反爬虫机制。毕竟这是用户正常访问时行为。
1 2 tencent_jobs1 = boss.queryjob(query='腾讯' , city=101280600 , industry=None , position=101301 ) tencent_jobs2 = boss.queryjobpage(query='腾讯' , city=101280600 , industry=None , position=101301 , page=2 )
返回:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [{'jobName': '天衍实验室-大数据及人工智能高级研究员', 'salaryDesc': '30-60K', 'jobLabels': ['深圳 南山区 科技园', '3-5年', '硕士'], 'brandName': '腾讯', 'brandIndustry': '互联网', 'id': 'ce6f957a5e4cb3100nZ43dm6ElU~', 'lid': '22Uetj173Sz.search.1'}, {'jobName': 'QQ信息流推荐算法工程师/研究员', 'salaryDesc': '20-40K·14薪', 'jobLabels': ['深圳 南山区 科技园', '经验不限', '本科'], 'brandName': '腾讯', 'brandIndustry': '互联网', 'id': 'a280e0b17a2aeded03Fz3dq_GFQ~', 'lid': '22Uetj173Sz.search.2'}, ...
github https://github.com/kenblikylee/kcrawler