import requests
import csv
from lxml import etree

# 请求的网址
url = 'https://www.zhaopin.com/sou/jl765/kw01O00U80EG06G03F01N0/p1?srccode=401801'

# 请求头
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0"
}

try:
    # 发起请求,获取文本数据
    response = requests.get(url, headers=headers)
    response.encoding = 'utf-8'  # 确保编码正确

    # 创建csv文件并写入数据
    with open('智联招聘数据.csv', mode='w', encoding='utf-8', newline='') as f:
        # 创建csv对象
        csv_writer = csv.writer(f)

        # 写入标题
        csv_writer.writerow(['岗位', '薪资要求', '学历要求', '工作经验',
                             '公司性质', '公司规模', '经营范围'])

        # 解析HTML
        html_data = etree.HTML(response.text)

        # 获取所有岗位信息块
        job_blocks = html_data.xpath('//*[@id="positionList-hook"]/div/div[1]/div[*]')

        # 遍历每个岗位信息块
        for block in job_blocks:
            # 提取岗位名称
            bt = block.xpath('.//div[1]/div[1]/div[1]/a/text()')
            bt = bt[0].strip() if bt else ''

            # 提取薪资要求
            xz = block.xpath('.//div[1]/div[1]/div[1]/p/text()')
            xz = xz[0].strip() if xz else ''

            # 提取学历要求
            xl = block.xpath('.//div[1]/div[1]/div[3]/div[3]/text()')
            xl = xl[0].strip() if xl else ''

            # 提取工作经验
            jy = block.xpath('.//div[1]/div[1]/div[3]/div[2]/text()')
            jy = jy[0].strip() if jy else ''

            # 提取公司性质
            gsxz = block.xpath('.//div[1]/div[2]/div[2]/div[1]/text()')
            gsxz = gsxz[0].strip() if gsxz else ''

            # 提取公司规模
            gsgm = block.xpath('.//div[1]/div[2]/div[2]/div[2]/text()')
            gsgm = gsgm[0].strip() if gsgm else ''

            # 提取经营范围
            jyfw = block.xpath('.//div[1]/div[2]/div[2]/div[3]/text()')
            jyfw = jyfw[0].strip() if jyfw else ''

            # 打印信息
            print(f'岗位: {bt}, 薪资要求: {xz}, 学历要求: {xl}, 工作经验: {jy}, '
                  f'公司性质: {gsxz}, 公司规模: {gsgm}, 经营范围: {jyfw}')

            # 写入CSV
            csv_writer.writerow([bt, xz, xl, jy, gsxz, gsgm, jyfw])

    print("数据已成功保存到招聘数据.csv")

except Exception as e:
    print(f"发生错误: {e}")

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐