// ==UserScript== // @name 集团客户数据抓取工具 // @namespace http://tampermonkey.net/ // @version 1.0 // @description 自动抓取集团客户表格数据并导出为CSV文件 // @author You // @match file:///*D:/yd-other/爱企查/demo/group_all.html* // @grant none // ==/UserScript== (function() { 'use strict'; // 创建黄色圆点按钮 function createYellowDotButton() { const button = document.createElement('div'); button.style.position = 'fixed'; button.style.right = '20px'; button.style.top = '20px'; button.style.width = '30px'; button.style.height = '30px'; button.style.backgroundColor = 'yellow'; button.style.borderRadius = '50%'; button.style.cursor = 'pointer'; button.style.zIndex = '9999'; button.style.boxShadow = '0 0 5px rgba(0,0,0,0.5)'; button.title = '点击抓取表格数据'; button.addEventListener('click', scrapeTableData); document.body.appendChild(button); } // 抓取表格数据并导出为CSV async function scrapeTableData() { try { // 获取所有表格行数据 const tableRows = document.querySelectorAll('.arco-table-tr:not(.arco-table-empty-row)'); if (tableRows.length <= 1) { // 只有表头或者没有数据 alert('未找到表格数据'); return; } // 提取表头 const headers = []; const headerRow = tableRows[0]; const headerCells = headerRow.querySelectorAll('.arco-table-th .arco-table-th-item-title'); headerCells.forEach(cell => { headers.push(cell.textContent.trim()); }); // 提取数据行 const dataRows = []; for (let i = 1; i < tableRows.length; i++) { const row = tableRows[i]; const cells = row.querySelectorAll('.arco-table-td .arco-table-cell-wrap-value'); const rowData = []; cells.forEach(cell => { // 处理嵌套的typography元素 const typographyElement = cell.querySelector('.arco-typography'); const text = typographyElement ? typographyElement.textContent.trim() : cell.textContent.trim(); rowData.push(text); }); // 如果行中有数据,则添加到结果中 if (rowData.some(cell => cell !== '')) { dataRows.push(rowData); } } // 构建CSV内容 let csvContent = headers.join(',') + '\n'; dataRows.forEach(row => { csvContent += row.map(cell => `"${cell.replace(/"/g, '""')}"`).join(',') + '\n'; }); // 下载CSV文件 downloadCSV(csvContent, '集团客户数据.csv'); alert(`成功抓取 ${dataRows.length} 条记录`); } catch (error) { console.error('抓取数据时发生错误:', error); alert('抓取数据时发生错误,请检查控制台'); } } // 下载CSV文件 function downloadCSV(csvContent, filename) { const blob = new Blob(['\uFEFF' + csvContent], { type: 'text/csv;charset=utf-8;' }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.setAttribute('href', url); link.setAttribute('download', filename); link.style.visibility = 'hidden'; document.body.appendChild(link); link.click(); document.body.removeChild(link); } // 页面加载完成后创建按钮 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', createYellowDotButton); } else { createYellowDotButton(); } })();