
每年的年初都是招聘的旺季,想跳槽的小伙伴是否想了解一下相关行业使用的技术有哪些,哪些知识要学习的。
该文件只要是爬去boss直聘的相关职位信息,程序运行时只需要输入查询的城市,职位,爬去页数就可以把数据保存到json
文件中,用于分析。
实现功能
输入城市名,行政区名,职位,爬取的页面,将爬取数据保存为json文件,以便后期数据可视化分析,
该文章不做数据可视化分析。
该案例需要的库
requests,re,json,os,lxml一、目标网站
https://www.zhipin.com二、分析网站
1、根据城市查询某职位的信息
https://www.zhipin.com/c101280600-p100103分析可得 101280600 为查询城市的编号
2、根据城市名加行政区查询某职位的信息
https://www.zhipin.com/c101280600-p100103/b_宝安区分析可得 b_XX 其中XX为城市行政区
三、查询到城市编号并保存为json文件
1、分析所有的请求 找到城市编号的数据来源
分析可得所有城市编号的数据来源链接为
https://www.zhipin.com/common/data/city.json城市编号爬取主要代码
def query_city():
'''
查询城市编号并保存
'''
url = 'https://www.zhipin.com/common/data/city.json'
data = json.loads(send_request(url))
city = {}
for item in data['data']['cityList']:
for it in item['subLevelModelList']:
city.update({it['name'] : it['code']})
with open('./boss_city.json', 'w', encoding='utf-8') as f:
try:
json_str = json.dumps(city, indent=4, ensure_ascii=False)
f.write(json_str)
except Exception as e:
print('-----')
print(e)
pass四、程序编码
1、加载程序用到的库以及全局变量定义
# here put the import lib
import requests,re,json
import os
from lxml import etree #使用xpath语法解析
base_url = "https://www.zhipin.com"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
}
# 正则表达式:去掉标签中的
和 标签,便于使用xpath解析提取文本
regx_obj = re.compile(r'
|<(em).*?>.*?<!--\1-->')
city_code = None #城市编号
city_region = None #城市行政区名2、入口程序
def main():
#全局变量修改需要global
global city_code
global city_region
city_list = get_city_json()
city_name = input('请输入需要爬取的城市:')
city_code = city_list[city_name]
city_region = input('请输入需要爬取的城市行政区:')
work = input('请输入需要爬取的职业信息:')
pages = int(input('请输入需要爬取的页面数:'))
for page in range(1, pages + 1):
start_work(page, work)
if __name__ == "__main__":
main()3、获取每页数据中详细页的链接
def detail_url(params):
'''
获取详细页的页面链接
'''
if city_region is None:
url_str = "c%s-p100103/?ka=sel-city-%s"%(city_code,city_code)
else:
url_str = "c%s-p100103/b_%s"%(city_code,city_region)
job_url = '/'.join([base_url, url_str])
html = send_request(job_url, param=params)
# 列表页页面
html_obj = etree.HTML(html)
# 提取详情页url地址
nodes = html_obj.xpath(".//div[@class='info-primary']//a/@href")
for node in nodes:
url = '/'.join([base_url, node])# 拼接成完整的url地址
print(url)
html = send_request(url)
html_obj = etree.HTML(html)
parse_data(html_obj)4、xpath解析爬取的详细页数据
def parse_data(html_obj):
'''
xpath 解析相关的数据
'''
# 解析为HTML文档
item = {}
# 职位名
item['position'] = html_obj.xpath("//div[@class='job-primary detail-box']/div[@class='info-primary']/div[@class='name']/h1/text()")[0]
# 发布者姓名
item['publisherName'] = html_obj.xpath("//div[@class='job-detail']//h2/text()")[0]
# 发布者职位
item['publisherPosition'] = html_obj.xpath("//div[@class='detail-op']//p/text()")[0]
# 薪水
item['salary'] = html_obj.xpath(".//div[@class='info-primary']//span[@class='salary']/text()")[0].strip()
# 工作职责
item['responsibility'] = html_obj.xpath("//div[@class='job-sec']//div[@class='text']/text()")[0].strip()
# 招聘要求
item['requirement'] = html_obj.xpath("//div[@class='job-primary detail-box']/div[@class='info-primary']/p/text()")[0]
# 招聘企业
item['company'] = html_obj.xpath("//div[@class='sider-company']/div[@class='company-info']/a/@title")[0].strip()
write_data(item) 5、保存解析的数据
def write_data(param):
'''
将数据写入文件中
'''
with open('./job.json', 'a+', encoding='utf-8') as f:
try:
job_json = json.dumps(param, indent=4, ensure_ascii=False)
f.write(job_json + ',')
except Exception as e:
print('-----')
print(e)
pass评论
暂无评论
评论(0)