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

相关文章

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句