爬取boss直聘简单案例

前提准备

以chrome自动化为例

  1. 下载浏览器驱动

  2. 查看chrome的版本

    • 设置->关于chrome
    • 如图116版本,大版本号要和驱动对应
    • 下载如图116稳定版
  1. 安装驱动
    • 将下载好的chromedriver.exe复制到python安装路径下的Scripts(如果没有这一步,需要在代码中手动配置路径)
  1. 安装相关第三方库,在终端输入如下

    shell 复制代码
    pip install selenium
    shell 复制代码
    pip install openpyxl  # 存储数据到excel

源代码

python 复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import openpyxl


class ZhipinSpider():
    def __init__(self):
        # 创建 Chrome 实例
        self.driver = webdriver.Chrome()

    def search_data(self, city, job):
        self.city = city
        self.job = job
        # 打开网页
        self.driver.get(f"https://www.zhipin.com/{self.city}")  # 请替换为您要访问的网页地址
        # 定位到具有特定 class 的输入框元素
        input_element = self.driver.find_element(By.CLASS_NAME, "ipt-search")
        # 向输入框输入内容
        input_element.send_keys(self.job)
        button_element = self.driver.find_element(By.CLASS_NAME, "btn-search")
        button_element.click()
        wait = WebDriverWait(self.driver, 10)
        job_list_elements = wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, "job-card-wrapper")))

        # 创建一个新的 Excel 工作簿和工作表
        wb = openpyxl.Workbook()
        ws = wb.active

        for index, job_element in enumerate(job_list_elements):
            company_name = job_element.find_element(By.CLASS_NAME, "company-name").text
            job_name = job_element.find_element(By.CLASS_NAME, "job-name").text
            job_area = job_element.find_element(By.CLASS_NAME, "job-area").text
            job_salary = job_element.find_element(By.CLASS_NAME, "salary").text
            taglist = job_element.find_element(By.CLASS_NAME, "tag-list").text.split('\n')
            job_career = taglist[0]
            job_edu = taglist[1]

            # 将数据写入工作表的行中
            ws.append([company_name, job_name, job_area, job_salary, job_career, job_edu])

        # 保存 Excel 文件
        wb.save(f'{self.city}.xlsx')

    def main(self):
        self.search_data('shanghai', '初级软件测试')


if __name__ == '__main__':
    zp = ZhipinSpider()
    zp.main()
    input("按下回车键以关闭浏览器...")
    zp.driver.quit()
  • 修改main函数参数可以选择爬取其他城市和其他岗位信息

常见报错

python 复制代码
raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: 
  • 可能原因:等待时间超过指定的时间但元素仍未出现,就会引发,一般与测试时网络情况有关
  • 解决办法:修改wait = WebDriverWait(self.driver, 10)参数增加等待时间

爬取结果

相关推荐
长空任鸟飞_阿康8 小时前
AI 多模态全栈应用项目描述
前端·vue.js·人工智能·node.js·语音识别
Mintopia8 小时前
🌐 实时协同 AIGC:多人在线 Web 创作的技术架构设计
前端·人工智能·trae
Mintopia8 小时前
🔥 “Solo Coding”的近期热度解析(截至 2025 年末)
前端·人工智能·trae
Alberta ゙9 小时前
C++初阶
开发语言·c++
YoungHong19929 小时前
【Python进阶】告别繁琐Debug!Loguru一键输出异常日志与变量值
python·debug·异常处理·日志·loguru·log·logger
the白勺9 小时前
RabbitMQ-基础-总结
开发语言·c#
顾安r9 小时前
11.14 脚本网页游戏 猜黑红
前端·javascript·游戏·flask·html
码码哈哈0.09 小时前
Vue 3 + Vite 集成 Spring Boot 完整部署指南 - 前后端一体化打包方案
前端·vue.js·spring boot
AiXed9 小时前
PC微信协议之nid算法
python·网络协议·算法·微信
Dev7z9 小时前
基于Matlab多目标粒子群优化的无人机三维路径规划与避障研究
开发语言·matlab·无人机