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

如何用Java/JS下载网页源码中无对应链接的Excel文件?

Solution for Downloading Excel from OpenML Dataset Page Using JavaScript

Got it, let's break down how to handle this Excel download scenario. Since the download link isn't visible in the page source and standard URLConnection tricks won't cut it, here are two reliable JavaScript approaches you can use:

1. Simulate a Click on the Excel Button

If you're running this code directly in the browser (like via the console or a userscript), the simplest way is to find the Excel button element and trigger a click event on it. Here's how:

// Wait for the page to fully load so the button is accessible
document.addEventListener('DOMContentLoaded', () => {
  // First, inspect the Excel button to get its correct selector (right-click > Inspect)
  // Common selectors might be a class, data attribute, or text match
  const excelButton = document.querySelector('button:contains("Excel")') || 
                      document.querySelector('a.download-btn[data-format="excel"]');

  if (excelButton) {
    // Trigger the click to start the download
    excelButton.click();
    console.log("Excel download started by simulating button click!");
  } else {
    console.error("Oops, couldn't locate the Excel download button. Double-check the selector.");
  }
});

Pro tip: Use your browser's dev tools to inspect the button element and update the querySelector parameter to match its actual attributes (like a specific class or data-* property) for better reliability.

2. Directly Construct the Download Request

If simulating a click isn't feasible (e.g., you're running code outside the page context), you can capture the actual download URL via your browser's network tab, then use JavaScript to trigger the download:

Step 1: Capture the Download URL

  1. Open your browser's DevTools (F12 or Ctrl+Shift+I)
  2. Go to the Network tab
  3. Scroll to the bottom of the OpenML page and click the "Excel" button
  4. Look for the new request that appears in the Network list—this is the actual Excel download endpoint. Copy its full URL.

Step 2: Use JavaScript to Trigger the Download

Once you have the URL, you can create a hidden anchor tag to start the download:

function initiateExcelDownload() {
  // Replace this with the actual URL you captured from the Network tab
  const excelDownloadUrl = "https://www.openml.org/api/v1/data/download/31/excel";
  const desiredFilename = "openml_dataset_31.xlsx";

  // Create a hidden anchor element
  const downloadLink = document.createElement("a");
  downloadLink.href = excelDownloadUrl;
  downloadLink.download = desiredFilename;
  downloadLink.style.display = "none";

  // Add to the page and trigger click
  document.body.appendChild(downloadLink);
  downloadLink.click();

  // Clean up after download starts
  document.body.removeChild(downloadLink);
}

// Run the function to start the download
initiateExcelDownload();

Handling CSRF Protection (If Needed)

Some sites use CSRF tokens to secure downloads. If the direct URL approach fails, check for a CSRF token in the page's meta tags, then use fetch to retrieve the file blob and trigger the download:

async function downloadExcelWithCSRF() {
  const excelDownloadUrl = "https://www.openml.org/api/v1/data/download/31/excel";
  const csrfToken = document.querySelector('meta[name="csrf-token"]')?.content;

  if (!csrfToken) {
    console.error("CSRF token not found—check page meta tags.");
    return;
  }

  try {
    const response = await fetch(excelDownloadUrl, {
      method: "GET",
      headers: {
        "X-CSRF-Token": csrfToken
      }
    });

    if (!response.ok) throw new Error(`Request failed: ${response.status}`);

    const blob = await response.blob();
    const blobUrl = window.URL.createObjectURL(blob);

    const downloadLink = document.createElement("a");
    downloadLink.href = blobUrl;
    downloadLink.download = "openml_dataset_31.xlsx";
    document.body.appendChild(downloadLink);
    downloadLink.click();

    // Clean up
    window.URL.revokeObjectURL(blobUrl);
    document.body.removeChild(downloadLink);
  } catch (err) {
    console.error("Download failed:", err);
  }
}

downloadExcelWithCSRF();

Both methods should work reliably—pick the one that fits your use case best!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:20:21