python实战案例:采集招聘网,知晓现今岗位要求

2022-04-07  本文已影响0人  颜狗一只

前言

嗨喽!大家好呀,这里是魔王~

今天我们来采集专业的互联网求职招聘网站。

致力于提供真实可靠的互联网岗位求职招聘找工作信息,拥有海量的互联网人才储备

环境介绍:

模块使用:

内置模块:

第三方模块:

win + R 输入cmd 输入安装命令 pip install 模块名 如果出现爆红 可能是因为 网络连接超时 切换国内镜像源

如何实现一个爬虫案例(爬虫基本流程思路):

一. 数据来源分析

  1. 确定自己想要获取数据是什么?
    爬取是什么网站, 网上上面什么数据
  2. 通过开发者工具, 进行抓包分析, 分析我们想要数据是来自于哪里

二. 代码实现步骤过程: 最基本四个步骤 发送请求,获取数据,解析数据,保存数据

  1. 发送请求, 对于刚刚分析的到url地址发送get请求 (并且模拟浏览器发送请求)
  2. 获取数据, 获取服务器返回response响应数据
  3. 解析数据, 提取我们想要数据内容, 招聘信息基本数据
  4. 保存数据, 把数据保存本地 表格

1、get 是从服务器上面获取数据 post 是向服务器传送数据

2、get请求参数,会直接显示在url链接上面, post在请求体里面的 隐性传递

代码

# # 导入数据数据请求模块
# import requests  # 第三方模块 需要 pip install requests
# # 导入正则
# import re
# import json
# # 导入格式化输出模块
# import pprint
# # 导入csv模块
# import csv
#
# f = open('招聘.csv', mode='a', encoding='utf-8', newline='')
# csv_writer = csv.DictWriter(f, fieldnames=[
#     '职位名字',
#     '公司名字',
#     '工作城市',
#     '学历要求',
#     '经验要求',
#     '薪资待遇',
#     '公司地址',
#     '公司规模',
#     '详情页',
# ])
# csv_writer.writeheader()  # 写入表头
# for page in range(1, 11):
#     # 1. 发送请求, 对于刚刚分析的到url地址发送get请求 (并且模拟浏览器发送请求)
#     url = f'https://www.lagou.com/wn/jobs?pn={page}&fromSearch=true&kd=python'
#     # headers 请求头 用伪装python代码 可以把python代码伪装浏览器去发送请求
#     # 通过request这个模块里面get请求方法 对于url地址发送请求, 并且携带上headers请求头伪装, 最后用自定义变量response接收返回数据
#     response = requests.get(url=url, headers=headers)
#     # <Response [200]>  <>对象的意思 response对象 200 状态码表示请求成功
#     # 2. 获取数据, 获取服务器返回response响应数据
#     # print(response.text)  # print() 打印函数 response.text 获取响应对象文本数据(获取网页源代码) 字符串数据类型
#     # 3. 解析数据提取我们想要数据 正则表达式在付费课程 2.5个小时讲解内容
#     """
#     <span>未融资</span>
#     <span>(.*?)</span>
#     () 精确匹配 表示自己想要数据内容  不加括号 泛匹配 可以匹配但是不要
#     . 匹配任意字符(除了换行符\n)  * 匹配前一个字符0个或者无限  ? 非贪婪匹配模式
#     re.findall('匹配规则', 从哪里找数据)  通过re里面findall的方法 从response.text里面去找寻关于
#     <script id="__NEXT_DATA__" type="application/json">(.*?)</script> 数据中 的(.*?) 是我们要提取出来的
#     """
#     html_data = re.findall('<script id="__NEXT_DATA__" type="application/json">(.*?)</script>', response.text)[0]
#     # print(html_data)  # 字符串数据 需要把字符串数据转成字典
#     json_data = json.loads(html_data)  # 把字符串(完整数据结构)转成字典
#     # print(json_data)
#     pprint.pprint(json_data)  # 格式化输出字典数据 展开效果
#     # 字典取值 根据冒号左边的内容(键), 提取冒号右边的内容(值)
#     for index in json_data['props']['pageProps']['initData']['content']['positionResult']['result']:
#         # pprint.pprint(index)
#         job_info = index['positionDetail'].replace('<br />', '').replace('<br>', '')  # 岗位职责
#         href = f'https://www.lagou.com/wn/jobs/{index["positionId"]}.html'
#         dit = {
#             '职位名字': index['positionName'],
#             '公司名字': index['companyFullName'],
#             '工作城市': index['city'],
#             '学历要求': index['education'],
#             '经验要求': index['workYear'],
#             '薪资待遇': index['salary'],
#             '公司地址': index['positionAddress'],
#             '公司规模': index['companySize'],
#             '详情页': href
#         }
#         csv_writer.writerow(dit)
#         title = index['positionName'] + index['companyFullName']
#         title = re.sub(r'[\/?:"<>|]', '', title)
#         # w 写入
#         with open('info\\' + title + '.txt', mode='w', encoding='utf-8') as f:
#             f.write(job_info)
#         print(dit)
#         break

import requests
import parsel
url = 'https://www.zhipin.com/c100010000/?query=python&page=3&ka=page-3'

response = requests.get(url=url, headers=headers)
# print(response.text)
selector = parsel.Selector(response.text)
lis = selector.css('.job-list ul li')
for li in lis:
    title = li.css('.job-name a::attr(title)').get()
    company_name = li.css('.company-text .name a::attr(title)').get()
    money = li.css('.job-limit .red::text').get()
    job_info = li.css('.job-limit p::text').getall()
    exp = job_info[0]
    edu = job_info[1]
    print(title, company_name, money, exp, edu)

尾语

好了,我的这篇文章写到这里就结束啦!

有更多建议或问题可以评论区或私信我哦!一起加油努力叭(ง •_•)ง

喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!

上一篇 下一篇

猜你喜欢

热点阅读