// ==UserScript==
// @name         河北住建厅公告提取
// @version      1.0
// @description  点击按钮提取河北住建厅信息,根据页面 URL 自动判断逻辑
// @author       YourName
// @match        https://zfcxjst.hebei.gov.cn/*
// @require      https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function () {
  'use strict';

  // 创建按钮
  const button = document.createElement('button');
  button.innerText = '提取公告';
  button.style.position = 'fixed';
  button.style.top = '10px';
  button.style.right = '10px';
  button.style.zIndex = '99999';
  button.style.padding = '8px 12px';
  button.style.backgroundColor = '#007bff';
  button.style.color = 'white';
  button.style.border = 'none';
  button.style.borderRadius = '4px';
  button.style.cursor = 'pointer';
  button.style.fontSize = '14px';
  button.style.boxShadow = '0 2px 4px rgba(0,0,0,0.2)';
  document.body.appendChild(button);

  // 函数:提取河北住建厅公告数据
  function extractHBZJTData_GongGaoGongShi(types) {
    console.log("页面标题:", document.title);
    const result = [];
    const contentDiv = document.querySelector('div.pson_listWenhao');
    if (!contentDiv) {
      console.log("未找到目标 div 元素");
      return;
    }
    contentDiv.querySelectorAll('li').forEach(li => {
      const aTag = li.querySelector('a');
      const dateSpan = li.querySelector('span.date');
      const wenhaoSpan = li.querySelector('span.wenhao');

      if (!aTag || !dateSpan) return;

      let href = aTag.getAttribute('href');
      let title = aTag.textContent.trim();

      let wenhao = '';
      if (wenhaoSpan) {
        wenhao = wenhaoSpan.textContent.trim();
      }
      let fullUrl = href;
      if (href.startsWith('/hbzjt')) {
        fullUrl = new URL(href, 'https://zfcxjst.hebei.gov.cn').href;
      }
      result.push({
        title: wenhao ? `[${wenhao}] ${title}` : title,
        url: fullUrl,
        date: dateSpan.textContent.trim(),
        type:types
      });
    });

    // result.forEach(item => {
    //    console.log(`${item.date}|${item.type} | ${item.title} -> ${item.url}`);
    // });
    // 发送数据到 API
    sendToAPI(result);
  }
  function sendToAPI(dataArray) {
    GM_xmlhttpRequest({
      method: 'POST',
      url: 'http://192.168.196.81:8081/sys_api/api/buildingspider/batch',
      data: JSON.stringify(dataArray),
      headers: {
        'Content-Type': 'application/json'
      },
      onload: function(response) {
        console.log('数据发送成功:', response.responseText);
        alert('数据已成功发送到服务器!');
      },
      onerror: function(error) {
        console.error('数据发送失败:', error);
        alert('数据发送失败,请检查网络或服务器状态!');
      }
    });
  }

  // 函数:提取河北住建厅公告数据
    function extractHBZJTData_XinWenZiXun(types) {
        console.log("页面标题:", document.title);
        const result = [];
        const contentDiv = document.querySelector('div.pson_list');
        if (!contentDiv) {
            console.log("未找到目标 div 元素");
            return;
        }
        contentDiv.querySelectorAll('li').forEach(li => {
            const aTag = li.querySelector('a');
            const dateSpan = li.querySelector('span.date');
            if (!aTag || !dateSpan) return;
            let href = aTag.getAttribute('href');
            let title = aTag.textContent.trim();

            let fullUrl = href;
            if (href.startsWith('/hbzjt')) {
                fullUrl = new URL(href, 'https://zfcxjst.hebei.gov.cn').href;
            }
            result.push({
                title: title,
                url: fullUrl,
                date: dateSpan.textContent.trim(),
                type:types
            });
        });

        // result.forEach(item => {
        //    console.log(`${item.date}|${item.type} | ${item.title} -> ${item.url}`);
        // });
        // 发送数据到 API
        sendToAPI(result);
    }

    // 备用函数(可根据需要自定义)
    function fallbackFunction() {
        console.log("不爬虫,页面标题:", document.title);
    }

    // 按钮点击事件
    button.addEventListener('click', () => {
        const currentUrl = window.location.href;

        if      ( currentUrl.includes('https://zfcxjst.hebei.gov.cn/hbzjt/zcwj/gggs/') ){
            extractHBZJTData_GongGaoGongShi("河北省住房和城乡建设厅,公告公示");
        }else if( currentUrl.includes('https://zfcxjst.hebei.gov.cn/hbzjt/zcwj/tfwj/') ){
            extractHBZJTData_GongGaoGongShi("河北省住房和城乡建设厅,厅发文件");
        }else if( currentUrl.includes('https://zfcxjst.hebei.gov.cn/hbzjt/zcwj/gfxwj/') ){
            extractHBZJTData_GongGaoGongShi("河北省住房和城乡建设厅,厅发规范性文件");
        }else if( currentUrl.includes('https://zfcxjst.hebei.gov.cn/hbzjt/xwzx/szyw/') ) {
            extractHBZJTData_XinWenZiXun("河北省住房和城乡建设厅,时政要闻")
        }else if( currentUrl.includes('https://zfcxjst.hebei.gov.cn/hbzjt/xwzx/jsyw/') ) {
            extractHBZJTData_XinWenZiXun("河北省住房和城乡建设厅,建设要闻")
        } else if( currentUrl.includes('https://zfcxjst.hebei.gov.cn/hbzjt/xwzx/sxdt/') ) {
            extractHBZJTData_XinWenZiXun("河北省住房和城乡建设厅,市县动态")
        }else if( currentUrl.includes('https://zfcxjst.hebei.gov.cn/hbzjt/xwzx/mtgz/') ) {
            extractHBZJTData_GongGaoGongShi("河北省住房和城乡建设厅,媒体关注")
        }else if( currentUrl.includes('https://zfcxjst.hebei.gov.cn/hbzjt/xwzx/zcjd/') ) {
            extractHBZJTData_XinWenZiXun("河北省住房和城乡建设厅,政策解读")
        } else {
            fallbackFunction();
        }
    });
})();
最后修改:2025 年 06 月 28 日
如果觉得我的文章对你有用,请随意赞赏