python爬取boss直聘职位数据,并保存到本地

2023-10-14 03:59

本文主要是介绍python爬取boss直聘职位数据,并保存到本地,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

代码环境

  1. python 3.7
  2. pip 19.0.3

主要引用的第三方库

  1. requests,用于模拟http/https请求
    • 安装: pip install requests
    • 文档: requests中文文档
  2. beautifulsoup4,用于解析网页,得出我们想要的内容。
    • 安装: pip install beautifulsoup4
    • 文档: bs4中文文档
  3. xlwt,将爬到的结果以Excel的形式保存到本地
    • 安装: pip install xlwt
    • api: xlwt api

打开网页

首先打开boss直聘官网,选择一个地点,然后输入关键字,点击搜索,这里以深圳、python为例。
在这里插入图片描述

观察地址栏URL,可以发现有四个参数,分别是query,city,industry和position,query和city很明显是我输入的python和选择的地点深圳;而industry和position也就是公司行业和职位类型,这里没有选择这两项。

分析网页

F12打开开发者工具
在这里插入图片描述
每一条职位信息都在一个<li>标签中,<li>标签下的<div class=“job-primary”>就是我们要找的内容。

代码

  • 获取城市编码

    url中的city=101280600,显示的是深圳,说明城市名有一个对应的编号,F12 点击Network选中XHR,有一个city.json
    在这里插入图片描述

import requests
from bs4 import BeautifulSoup
import json
import xlwt
import time
import randomuser_agent_list = ["Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36","Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/61.0","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)","Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15"
]headers = {"user-agent": random.choice(user_agent_list)}# 获取指定城市的编码
def get_city_code(city_name):response = requests.get("https://www.zhipin.com/wapi/zpCommon/data/city.json")contents = json.loads(response.text)cities = contents["zpData"]["hotCityList"]city_code = contents["zpData"]["locationCity"]["code"]for city in cities:if city["name"] == city_name:city_code = city["code"]return city_codedef get_url(query="", city="", industry="", position="", page=1):base_url = "https://www.zhipin.com/job_detail/?query={}&city={}&industry={}&position={}&page={}"urls = []url = base_url.format(query, city, industry, position, page)response = requests.get(url, headers=headers)soup = BeautifulSoup(response.text, "lxml")page_list = soup.find("div", "page").find_all("a")urls.append(url)while page_list[len(page_list) - 1]["href"] != "javascript:;":page += 1url = base_url.format(query, city, industry, position, page)urls.append(url)response = requests.get(url, headers=headers)soup = BeautifulSoup(response.text, "lxml")page_list = soup.find("div", "page").find_all("a")return urlsdef get_html(url):response = requests.get(url, headers=headers)return response.textdef job_info(job_name, company, industry, finance, staff_number, salary, site, work_experience, education_bak, job_desc):return {"job_name": job_name,"company": company,"industry": industry,"finance": finance,"staff_number": staff_number,"salary": salary,"site": site,"work_experience": work_experience,"education_bak": education_bak,"job_desc": job_desc}def get_job_desc(jid, lid):url = "https://www.zhipin.com/wapi/zpgeek/view/job/card.json?jid={}&lid={}"response = requests.get(url.format(jid, lid), headers=headers)html = json.loads(response.text)["zpData"]["html"]soup = BeautifulSoup(html, "lxml")desc = soup.find("div", "detail-bottom-text").get_text()return descdef get_content(html):bs = BeautifulSoup(html, 'lxml')contents = []for info in bs.find_all("div", "job-primary"):job_name = info.find("div", "job-title").get_text()company = info.find("div", "company-text").a.get_text()jid = info.find("div", "info-primary").a["data-jid"]lid = info.find("div", "info-primary").a["data-lid"]desc = get_job_desc(jid, lid)texts = [text for text in info.find("div", "info-primary").p.stripped_strings]site = texts[0]work_exp = texts[1]edu_bak = texts[2]salary = info.span.get_text()companies = [text for text in info.find("div", "company-text").p.stripped_strings]industry = companies[0]if len(companies) > 2:finance = companies[1]staff_num = companies[2]else:finance = Nonestaff_num = companies[1]contents.append(job_info(job_name, company, industry, finance, staff_num, salary, site, work_exp, edu_bak, desc))time.sleep(1)return contentsdef save_data(content, city, query):file = xlwt.Workbook(encoding="utf-8", style_compression=0)sheet = file.add_sheet("job_info", cell_overwrite_ok=True)sheet.write(0, 0, "职位名称")sheet.write(0, 1, "公司名称")sheet.write(0, 2, "行业")sheet.write(0, 3, "融资情况")sheet.write(0, 4, "公司人数")sheet.write(0, 5, "薪资")sheet.write(0, 6, "工作地点")sheet.write(0, 7, "工作经验")sheet.write(0, 8, "学历要求")sheet.write(0, 9, "职位描述")for i in range(len(content)):sheet.write(i+1, 0, content[i]["job_name"])sheet.write(i+1, 1, content[i]["company"])sheet.write(i+1, 2, content[i]["industry"])sheet.write(i+1, 3, content[i]["finance"])sheet.write(i+1, 4, content[i]["staff_number"])sheet.write(i+1, 5, content[i]["salary"])sheet.write(i+1, 6, content[i]["site"])sheet.write(i+1, 7, content[i]["work_experience"])sheet.write(i+1, 8, content[i]["education_bak"])sheet.write(i+1, 9, content[i]["job_desc"])file.save(r'c:\projects\{}_{}.xls'.format(city, query))def main():city_name = "深圳"city = get_city_code(city_name)query = "python"urls = get_url(query=query, city=city)contents = []for url in urls:html = get_html(url)content = get_content(html)contents += contenttime.sleep(5)save_data(contents, city_name, query)if __name__ == '__main__':main()

这篇关于python爬取boss直聘职位数据,并保存到本地的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/207984

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互