gen_000xx.py

gen_000xx.py — 学校专属解析函数(示例)

每所学校一个文件,实现统一接口 extract_table_from_html(htmlcontext, tempfile),输出统一 JSON(announcement_name / publish_time / link)。

代码

import json
from bs4 import BeautifulSoup

def extract_table_from_html(htmlcontext, tempfile):
    """
    从招聘列表HTML中提取:公告名称、发布时间、链接、公司名称
    写入JSON文件
    """
    soup = BeautifulSoup(htmlcontext, 'html.parser')
    result_list = []

    # 查找所有列表项
    info_lists = soup.find_all('ul', class_='infoList')
    
    for ul in info_lists:
        announcement_name = None
        publish_time = None
        link = None
        hd_company = ""  # 公司名称(按你的字段要求)

        # 提取标题 + 链接
        name_tag = ul.find('li', class_='span7')
        if name_tag and name_tag.a:
            announcement_name = name_tag.a.get_text(strip=True)
            link = name_tag.a.get('href', '').strip()

        # 提取时间
        time_tag = ul.find('li', class_='span4')
        if time_tag:
            publish_time = time_tag.get_text(strip=True)

        # 有效数据才加入
        if announcement_name and publish_time and link:
            result_list.append({
                "announcement_name": announcement_name,
                "publish_time": publish_time,
                "link": link,
                "hd_company": hd_company  # 4个字段齐全
            })

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

项目分区导航func_gen_bygpt ⬅️ | 03-gen_000xx | ➡️ auto_gen_com