gen_00001.py

gen_00001.py — 大公司专属解析函数(示例)

实现统一接口 extract_table_from_html(htmlcontext, tempfile) 的大公司版解析函数。分工背景见 auto_gen_com 主篇

代码

import json
from bs4 import BeautifulSoup

# 从 HTML 中提取职位列表信息,输出指定格式的 JSON 文件
def extract_table_from_html(htmlcontext, tempfile):
    # 初始化 BeautifulSoup 解析器
    soup = BeautifulSoup(htmlcontext, 'html.parser')
    # 存储最终解析结果
    announcements = []

    # 遍历所有职位列表项
    for item in soup.find_all(class_='list-item-main'):
        # 提取职位名称
        name = item.find(class_='pos-name').get_text(strip=True)
        # 提取发布时间
        publish_time = item.find(class_='pos-pubTime').get_text(strip=True)

        # 提取部门 / 机构信息(优先 pos-department,没有则取 pos-company)
        dept_name = ''
        if item.find(class_='pos-department'):
            dept_name = item.find(class_='pos-department').get_text(strip=True)
        if dept_name == "" and item.find(class_='pos-company'):
            dept_name = item.find(class_='pos-company').get_text(strip=True)

        # 提取职位类别
        job_category_name = ''
        if item.find('div', class_='pos-cate'):
            job_category_name = item.find('div', class_='pos-cate').get_text(strip=True)

        # 提取工作地点(优先 pos-locate,没有则取 pos-workPlace)
        loc_name = ''
        if item.find(class_='pos-locate'):
            loc_name = item.find(class_='pos-locate').get_text(strip=True)
        elif loc_name == '' and item.find(class_='pos-workPlace'):
            loc_name = item.find(class_='pos-workPlace').get_text(strip=True)

        # 提取薪资
        salary = ""
        salary_tag = item.find(class_='pos-salary')
        if salary_tag:
            salary = salary_tag.get_text(strip=True)

        # 提取招聘人数
        job_num = ""
        job_tag = item.find(class_='pos-num')
        if job_tag:
            job_num = job_tag.get_text(strip=True)

        # 链接(当前未提取,留空)
        link = ""

        # 组装单条职位数据
        announcements.append({
            "announcement_name": name,
            "publish_time": publish_time,
            "link": link,
            "hd_dept": dept_name,
            "hd_loc": loc_name,
            "hd_job_num": job_num,
            "hd_job_category": job_category_name,
            "hd_salary": salary
        })

    # 将解析结果写入 JSON 文件
    with open(tempfile, 'w', encoding='utf-8') as f:
        json.dump(announcements, f, ensure_ascii=False, indent=4)

项目分区导航func_gen_bygpt ⬅️ | 04-gen_00001 | ➡️ page_00001