换坑季-51Job前程无忧 Python爬虫

2023-11-11 07:10

本文主要是介绍换坑季-51Job前程无忧 Python爬虫,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

写了个简易的Python爬虫,实现对目的工作的分析。
说明,只用了正则re库进行数据处理,requests进行请求,开了4个简易的函数线程。
url是以下界面的url:
在这里插入图片描述
主要实现了以下CSV功能:
在这里插入图片描述
全部代码:

import requests
import re
import csv
from threading import Threaddef req(i):count = 1try:for url in i:headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'}response = requests.get(url=url, headers=headers, timeout=5)response.encoding = 'GBK'content = re.findall(r'<a target="_blank" title="(.*?)" href="(.*?)" onmousedown="">.*?<span class="t2"><a target="_blank" title="(.*?)" href=".*?">.*?</a></span>.*?<span class="t3">(.*?)</span>.*?<span class="t4">(.*?)</span>.*?<span class="t5">(.*?)</span>', response.text, re.S)# print(content)for index in content:txt = []title = index[0]thisUrl = index[1]try:thisContent = requests.get(url=thisUrl, headers=headers, timeout=5)text = re.findall(r'<div class="bmsg job_msg inbox">(.*?)<div class="mt10">', thisContent.text, re.S)final = ''.join(text).replace('\r\n\t\t\t\t\t\t', '').replace('<p>', '').replace('</p>', '').replace('<span>', '').replace('</span>', '').replace('<br>', '').replace('&nbsp;', '').replace('</div>', '').replace('<div>', '').replace('\t\t\t\t\t\t\t\t\t\t\t\t', '').replace('<b>', '').replace('</b>', '').replace('</li>', '').replace('</strong>', '').replace('<strong>', '').replace('<li>', '')company = index[2]area = index[3]salary = index[4]date = index[5]txt.append(title)txt.append(thisUrl)txt.append(final)txt.append(company)txt.append(area)txt.append(salary)txt.append(date)# print(txt)with open(r'./前程无忧.csv', 'a', newline='', encoding='utf-8-sig') as csvf:spanwriter = csv.writer(csvf)spanwriter.writerow(txt)count = count + 1except:print("此次请求详情失败!!!" + thisUrl)except:print('请求首页失败!' + i)print("共%d多少条信息~" % count)if __name__ == '__main__':txt1 = []txt2 = []txt3 = []txt4 = []url = 'https://search.51job.com/list/090200,000000,0000,00,9,99,%25E8%25BF%2590%25E7%25BB%25B4%25E5%25B7%25A5%25E7%25A8%258B%25E5%25B8%2588,2,{}.html?lang=c&stype=1&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare='for i in range(1, 6):i = url.format(i)txt1.append(i)for j in range(6, 12):j = url.format(j)txt2.append(j)for k in range(12, 18):k = url.format(k)txt3.append(k)for k in range(18, 24):k = url.format(k)txt3.append(k)t1 = Thread(target=req, args=(txt1,))t1.start()print('t1线程开始!')t2 = Thread(target=req, args=(txt2,))t2.start()print('t2线程开始!')t3 = Thread(target=req, args=(txt3,))t3.start()print('t3线程开始!')t4 = Thread(target=req, args=(txt4,))t4.start()print('t4线程开始!')

上面这个开了4个线程的代码其实对爬虫还是不太友好。
以下代码可以在详细页进行爬虫,建议使用生产者消费者模式。

import re
import requestsurl = 'https://jobs.51job.com/chengdu-jjq/114069603.html?s=01&t=0'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'}
response = requests.get(url=url, headers=headers, timeout=5)
response.encoding = 'GBK'
# 工作职位
job = re.findall(r'<h1 title="(.*?)">', response.text)
print(job)
# 薪资
salary = re.findall(r'<h1 title=".*?">.*?<input value="\d+" name="hidJobID" id="hidJobID" type="hidden" jt="0">.*?<strong>(.*?)</strong>', response.text, re.S)
print(salary)
# 公司名称
company = re.findall(r'target="_blank" title="(.*?)" class="catn">', response.text)
print(company)
# 公司性质
flag = re.findall(r'<p class="at" title="(.*?)"><span class="i_flag"></span>.*?</p>', response.text)
print(flag)
# 公司规模
people = re.findall(r'<p class="at" title="(.*?)"><span class="i_people"></span>.*?</p>', response.text)
print(people)
# 职位详细的内容:
content = re.findall(r'<p class="msg ltype" title="(.*?)">.*?</p>', response.text, re.S)
# 将正则匹配的内容进行字符串处理
content_str = ''.join(content)
# 城市
txt_city = re.findall(r'.*?(成都.*?)&nbsp.*?', content_str)
print(txt_city)
# 招多少人
txt_count = re.findall(r'.*?(招\d人).*?', content_str)
print(txt_count)
# 经验
txt_experience = re.findall(r'.*?(无工作经验).*?', content_str)
if len(txt_experience) == 0:txt_experience = re.findall(r'.*?(\d+年经验).*?', content_str)
print(txt_experience)
# 发布日期
txt_date = re.findall(r'.*?(\d+-\d+发布).*?', content_str)
print(txt_date)
# 学历要求, 只匹配了大专和本科
txt_education = re.findall(r'.*?(本科).*?', content_str)
if len(txt_education) == 0:txt_education = re.findall(r'.*?(大专).*?', content_str)
else:txt_education = '无学历要求'
print(txt_education)
# 职位招聘要求内容描述:
descrition = re.findall(r'<div class="bmsg job_msg inbox">(.*?)<div class="mt10">', response.text, re.S)
descrition = ''.join(descrition).replace('\r\n\t\t\t\t\t\t', '').replace('<p>', '').replace('</p>', '').replace('<span>', '').replace('</span>', '').replace('<br>', '').replace('&nbsp;', '').replace('</div>', '').replace('<div>', '').replace('\t\t\t\t\t\t\t\t\t\t\t\t', '').replace('<b>', '').replace('</b>', '').replace('</li>', '').replace('</strong>', '').replace('<strong>', '').replace('<li>', '')
print(descrition)

这篇关于换坑季-51Job前程无忧 Python爬虫的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python正则表达式匹配和替换的操作指南

《Python正则表达式匹配和替换的操作指南》正则表达式是处理文本的强大工具,Python通过re模块提供了完整的正则表达式功能,本文将通过代码示例详细介绍Python中的正则匹配和替换操作,需要的朋... 目录基础语法导入re模块基本元字符常用匹配方法1. re.match() - 从字符串开头匹配2.

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

通过Docker容器部署Python环境的全流程

《通过Docker容器部署Python环境的全流程》在现代化开发流程中,Docker因其轻量化、环境隔离和跨平台一致性的特性,已成为部署Python应用的标准工具,本文将详细演示如何通过Docker容... 目录引言一、docker与python的协同优势二、核心步骤详解三、进阶配置技巧四、生产环境最佳实践

Python一次性将指定版本所有包上传PyPI镜像解决方案

《Python一次性将指定版本所有包上传PyPI镜像解决方案》本文主要介绍了一个安全、完整、可离线部署的解决方案,用于一次性准备指定Python版本的所有包,然后导出到内网环境,感兴趣的小伙伴可以跟随... 目录为什么需要这个方案完整解决方案1. 项目目录结构2. 创建智能下载脚本3. 创建包清单生成脚本4

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e