获取贝壳新房列表

1、获取本页新房列表

javascript 复制代码
// 贝壳新房名称提取,自动复制到剪贴板
const names = Array.from(document.querySelectorAll('.resblock-name .name, .building-wrapper .name'))
  .map(el => el.textContent.trim());
copy(names.join('\n'));
console.log('✅ 已复制以下内容到剪贴板:\n' + names.join('\n'));

2、获取所有页的新房列表自动翻下一页,带暂停导出按钮

javascript 复制代码
// === 修复版:无行号污染 + 控制台纯文本输出 + 页面按钮 ===
let collectedData = [];
let isRunning = true;
let pageCount = 0;

// 1. 创建页面控制按钮
function createControlPanel() {
  const panel = document.createElement('div');
  panel.id = 'fang-collector-panel';
  panel.style.cssText = `
    position: fixed; top: 10px; right: 10px; z-index: 99999;
    background: #fff; padding: 10px; border-radius: 6px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.2); display: flex; gap: 8px;
  `;

  const pauseBtn = document.createElement('button');
  pauseBtn.id = 'pause-btn';
  pauseBtn.textContent = '暂停采集';
  pauseBtn.style.cssText = `
    padding: 6px 12px; border: none; border-radius: 4px;
    background: #ff9500; color: #fff; cursor: pointer;
  `;
  pauseBtn.onclick = () => {
    isRunning = !isRunning;
    pauseBtn.textContent = isRunning ? '暂停采集' : '继续采集';
    pauseBtn.style.background = isRunning ? '#ff9500' : '#4CAF50';
    if (isRunning) {
      console.log('▶️ 继续采集...');
      goToNextPage();
    } else {
      console.log('⏸️ 已暂停');
    }
  };

  const exportBtn = document.createElement('button');
  exportBtn.textContent = '导出Excel';
  exportBtn.style.cssText = `
    padding: 6px 12px; border: none; border-radius: 4px;
    background: #2196F3; color: #fff; cursor: pointer;
  `;
  exportBtn.onclick = () => exportToCSV();

  panel.appendChild(pauseBtn);
  panel.appendChild(exportBtn);
  document.body.appendChild(panel);
}

// 2. 采集当前页数据,同时用纯文本输出(避免控制台行号污染)
function collectCurrentPage() {
  const listItems = document.querySelectorAll('.resblock-list');
  const hotItems = document.querySelectorAll('.building-list-item');
  const currentPageNames = [];

  listItems.forEach(item => {
    const name = item.querySelector('.resblock-name .name')?.textContent.trim();
    if (!name) return;
    currentPageNames.push(name);
    collectedData.push({
      名称: name,
      均价: item.querySelector('.main-price .number')?.textContent.trim() + ' ' + (item.querySelector('.main-price .desc')?.textContent.trim() || ''),
      总价范围: item.querySelector('.second')?.textContent.trim() || '',
      户型: item.querySelector('.resblock-room')?.textContent.trim().replace(/\s+/g, ' ') || '',
      地址: item.querySelector('.resblock-location')?.textContent.trim() || '',
      标签: Array.from(item.querySelectorAll('.resblock-tag span')).map(t => t.textContent.trim()).join(', '),
      来源: '主列表'
    });
  });

  hotItems.forEach(item => {
    const name = item.querySelector('.building-wrapper .name')?.textContent.trim();
    if (!name) return;
    currentPageNames.push(name);
    collectedData.push({
      名称: name,
      均价: item.querySelector('.price-wrapper .price')?.textContent.trim() + ' ' + (item.querySelector('.price-wrapper .unit')?.textContent.trim() || ''),
      总价范围: '',
      户型: '',
      地址: '',
      标签: `${item.querySelector('.building-wrapper .type')?.textContent.trim()}, ${item.querySelector('.building-wrapper .status')?.textContent.trim()}`,
      来源: '热门楼盘'
    });
  });

  // 用console.log一次性打印整页所有名称,控制台只会输出文本,不会带行号
  console.log(`\n===== 第 ${pageCount + 1} 页楼盘 =====`);
  console.log(currentPageNames.join('\n'));
  console.log(`✅ 第 ${pageCount + 1} 页采集完成,当前共收集 ${collectedData.length} 个楼盘`);

  pageCount++;
}

// 3. 自动翻页
function goToNextPage() {
  if (!isRunning) return;

  collectCurrentPage();
  const nextBtn = document.querySelector('.page-box .next:not(.disabled)');
  if (nextBtn) {
    console.log('➡️ 点击下一页...\n');
    nextBtn.click();
    setTimeout(goToNextPage, 1500);
  } else {
    console.log('✅ 已到达最后一页,采集完成!');
    document.getElementById('pause-btn').textContent = '采集完成';
    document.getElementById('pause-btn').disabled = true;
    exportToCSV();
  }
}

// 4. 导出CSV
function exportToCSV() {
  if (collectedData.length === 0) {
    console.log('❌ 没有采集到任何数据');
    return;
  }
  const headers = Object.keys(collectedData[0]);
  const rows = collectedData.map(row => 
    headers.map(key => `"${(row[key] || '').replace(/"/g, '""')}"`).join(',')
  );
  const csvContent = '\uFEFF' + [headers.join(','), ...rows].join('\n');
  const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
  const url = URL.createObjectURL(blob);
  const link = document.createElement('a');
  link.href = url;
  link.download = `贝壳新房数据_${new Date().toLocaleDateString()}.csv`;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  URL.revokeObjectURL(url);
  console.log(`📥 已导出 ${collectedData.length} 条数据到CSV文件!`);
}

// 5. 启动
createControlPanel();
console.log('🚀 采集开始!页面右上角有【暂停/继续】和【导出Excel】按钮');
goToNextPage();
相关推荐
oil欧哟3 分钟前
我做了一个 Vibe Coding 术语学习站:VibeHub
前端·ai·agent·独立开发·vibe coding
审小匠OpenCPAi1 小时前
银行流水核查怎么自动化?单边匹配、双向勾稽与图聚类异常检测的工程对比
java·前端·人工智能·python·审计
Revolution611 小时前
一个公共表格组件,是怎么一步步失控的
前端·前端工程化
mONESY1 小时前
React useState 核心原理与最佳实践:从异步更新到性能优化全解
javascript
腻害兔1 小时前
【若依项目-产品经理视角】RuoYi-Vue-Pro 源码拆解:CRM 客户关系模块深度解析——从线索到回款,一套完整的 B2B 销售闭环是怎么搭的?
java·前端·javascript·vue.js·产品经理·ai编程
whyfail1 小时前
前端学 Spring Boot(2):一次点击,如何穿过整个后端?
前端·spring boot·后端
AI多Agent协作实战派2 小时前
AI多Agent协作系统实战(二十三):Agent读HEARTBEAT.md不读AGENTS.md——openclaw的文件加载之谜
前端·数据库·人工智能·uni-app
gis开发之家2 小时前
《Vue3 从入门到大神37篇》Vue3 源码详解(七):watch 与 watchEffect 源码对比——副作用是如何被追踪的?
javascript·前端框架·vue3·vue3源码
2601_955760073 小时前
如何用 Claude Opus 5 API 批量扩展长尾关键词和文章选题
前端·python·搜索引擎
KaMeidebaby3 小时前
卡梅德生物技术快报|bli亲和力检测gst:告别批量跑胶:BLI实时酶切监测技术加速GST融合蛋白下游流程优化
前端·网络·数据库·人工智能·算法