Python爬取BOSS直聘年薪30万互联网岗位信息 - 代码示例
由于BOSS直聘网站需要登录才能查看年薪为30万的岗位信息,因此需要先登录BOSS直聘网站,然后再进行爬取。
以下是一个示例代码,可以爬取BOSS直聘年薪30万的互联网行业岗位信息,并且按字段存储到表格中。
import requests
import json
import time
import pandas as pd
# 登录BOSS直聘网站
session = requests.Session()
login_url = 'https://login.zhipin.com/?ka=header-login'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36',
'referer': 'https://www.zhipin.com/',
}
session.get(login_url, headers=headers)
login_data = {
'password': 'your_password',
'username': 'your_username',
'remember': 'false',
}
login_url = 'https://passport.zhipin.com/account/login'
response = session.post(login_url, data=login_data, headers=headers)
if response.status_code == 200:
print('登录成功')
else:
print('登录失败')
# 爬取BOSS直聘年薪30万的互联网行业岗位信息
url = 'https://www.zhipin.com/c101010100-p100109/?salary=30&salary_provid=2'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36',
'referer': 'https://www.zhipin.com/',
}
response = session.get(url, headers=headers)
html = response.text
# 解析岗位信息
job_list = []
soup = BeautifulSoup(html, 'html.parser')
jobs = soup.select('.job-primary')
for job in jobs:
job_name = job.select_one('.job-title').get_text().strip()
salary = job.select_one('.red').get_text().strip()
work_time = job.select_one('.info-primary p').get_text().strip()
if '双休' in work_time:
is_weekend_off = '是'
else:
is_weekend_off = '否'
welfare = job.select_one('.info-company p').get_text().strip()
requirement = job.select_one('.job-desc').get_text().strip()
description = job.select_one('.info-publis .name').get_text().strip()
job_info = {
'岗位名称': job_name,
'月薪': salary,
'工时': work_time,
'是否双休': is_weekend_off,
'节假日福利': welfare,
'岗位要求': requirement,
'说明': description,
}
job_list.append(job_info)
# 存储到表格中
df = pd.DataFrame(job_list)
df.to_excel('job_info.xlsx', index=False)
print('岗位信息已保存到 job_info.xlsx 文件中')
需要注意的是,由于BOSS直聘网站的页面结构可能会随时改变,因此以上代码仅供参考,具体实现需要根据实际情况进行调整。同时,为了避免对BOSS直聘网站造成过大的访问压力,建议设置适当的爬取间隔时间。
原文地址: https://www.cveoy.top/t/topic/lUvu 著作权归作者所有。请勿转载和采集!