You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何导出网页HTML表格数据至.csv格式(含指定表格导出需求)

Hey there, let's tackle both your general and specific questions about exporting HTML tables to CSV—perfect for your thesis project!

1. General: How to Export HTML Table Data to CSV

Here are three reliable methods depending on your technical comfort level:

  • Browser Console Script (Most Flexible)
    Open your browser's DevTools (hit F12 or Ctrl+Shift+I), switch to the Console tab, and run this script. It grabs the first table on the page and downloads it as a CSV:

    function tableToCSV(table) {
      const rows = Array.from(table.querySelectorAll('tr'));
      return rows.map(row => {
        const cells = Array.from(row.querySelectorAll('td, th'));
        return cells.map(cell => {
          let text = cell.textContent.trim();
          // Handle commas/quotes to comply with CSV standards
          if (text.includes(',') || text.includes('"')) {
            text = `"${text.replace(/"/g, '""')}"`;
          }
          return text;
        }).join(',');
      }).join('\n');
    }
    
    function downloadCSV(csv, filename) {
      const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
      const url = URL.createObjectURL(blob);
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', filename);
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
    
    // Grab first table and export
    const targetTable = document.querySelector('table');
    if (targetTable) {
      const csv = tableToCSV(targetTable);
      downloadCSV(csv, 'table-data.csv');
    } else {
      console.log('No table found on the page');
    }
    
  • Browser Extension (For Non-Technical Users)
    Use a trusted table export extension from your browser's store. Most will scan the page for all tables, let you pick the one you want, and export it directly to CSV. Just stick to well-reviewed extensions to avoid privacy risks.

  • Copy-Paste to Spreadsheet (Quick Fix)
    Select the entire table, copy it, paste into Excel/Google Sheets, then export the sheet as CSV. Note: This can mess up merged cells or complex formatting, so it's best for simple tables.

2. Specific: Export Target Table from the Indian Equities Page

Since you need only the table with id="cross_rate_markets_stocks_1" (and avoid grabbing the whole site's data), follow these steps:

  1. Open the page, select any option from the dropdown that defaults to "BS...", and wait for the table to fully load.
  2. Open DevTools (F12/Ctrl+Shift+I) and go to the Console tab.
  3. Run this custom script—it's tailored to target only your desired table:
    function tableToCSV(table) {
      const rows = Array.from(table.querySelectorAll('tr'));
      return rows.map(row => {
        const cells = Array.from(row.querySelectorAll('td, th'));
        return cells.map(cell => {
          let text = cell.textContent.trim();
          // Clean up special characters for valid CSV
          if (text.includes(',') || text.includes('"')) {
            text = `"${text.replace(/"/g, '""')}"`;
          }
          return text;
        }).join(',');
      }).join('\n');
    }
    
    function downloadCSV(csv, filename) {
      const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
      const url = URL.createObjectURL(blob);
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', 'indian-equities-data.csv');
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
    
    // Target the exact table by ID
    const targetTable = document.getElementById('cross_rate_markets_stocks_1');
    if (targetTable) {
      const csv = tableToCSV(targetTable);
      downloadCSV(csv, 'indian-equities-data.csv');
    } else {
      console.log('Target table not found—wait for the page to finish loading after selecting the dropdown!');
    }
    
  4. Hit Enter, and your browser will download a CSV file named indian-equities-data.csv with only the table data you need.

Pro Tips:

  • Always wait for the table to finish rendering after selecting the dropdown before running the script.
  • If the script fails, try refreshing the page or using incognito mode (some sites have light anti-scraping measures that incognito can bypass).

内容的提问来源于stack exchange,提问作者Mankdavix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:42:20