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

如何将HTML表格抓取至Google Sheets并提取Forward Dividend数据?

Absolutely! Both of your requests are completely doable with Google Sheets' native tools and a bit of optional scripting. Let’s break down each task with clear, actionable steps:

1. Scraping HTML Tables into Google Sheets

Google Sheets has a built-in function specifically tailored for this: IMPORTHTML. Here’s how to use it:

  • In an empty cell, enter the formula:
    =IMPORTHTML("your-target-url", "table", table-index)
    
    • Replace "your-target-url" with the URL of the page containing the HTML table you want to grab.
    • "table" tells the function you’re targeting a table (you can also use "list" if you need to scrape unordered/ordered lists instead).
    • table-index is the position of the table on the page—start counting from 1. If the page has 3 tables, use 1, 2, or 3 to pick the exact one you need.

Pro tips:

  • If the table doesn’t load right away, give it a few seconds—Google Sheets needs time to fetch and parse the external data.
  • Some websites block direct scraping via these functions; check the site’s robots.txt file first to make sure you’re allowed to pull their content.
  • If the source table updates regularly, the function will auto-refresh the data in your sheet (you can tweak refresh settings in Sheets if needed).
2. Extracting Forward Dividend Data & Inserting into a Specific Field

For this, you can use IMPORTXML (another native function) to pull specific data points from Yahoo Finance and StreetInsider. If you need more control (like handling sites that block basic scraping), Google Apps Script is a great alternative.

Option 1: Using IMPORTXML (No Coding Required)

Yahoo Finance (AAPL Example)

In the exact cell where you want the Forward Dividend value to appear, enter:

=IMPORTXML("https://finance.yahoo.com/quote/AAPL?p=AAPL", "//span[contains(text(), 'Forward Dividend')]/following-sibling::span")

This XPath targets the "Forward Dividend" label and grabs the adjacent span that holds the numerical value (0.82 for AAPL).

StreetInsider (AAPL Example)

Similarly, use this formula to pull the Forward Dividend from StreetInsider’s dividend history page:

=IMPORTXML("https://www.streetinsider.com/dividend_history.php?q=aapl", "//td[contains(text(), 'Forward Dividend')]/following-sibling::td")

Note: Website HTML structures can change over time. If the formula stops working, right-click the Forward Dividend value on the page, select "Inspect", and update the XPath to match the current element structure.

Option 2: Google Apps Script (For More Robust Scraping)

If native functions fail (e.g., the site uses JavaScript to load data or blocks direct scraping), use a script to fetch and parse the data manually:

  1. Open your Google Sheet, go to Extensions > Apps Script.
  2. Replace the default code with this example (adjust URLs and regex patterns if the page structure changes):
    function getForwardDividend() {
      // Fetch Yahoo Finance page for AAPL
      const yahooUrl = "https://finance.yahoo.com/quote/AAPL?p=AAPL";
      const yahooResponse = UrlFetchApp.fetch(yahooUrl);
      const yahooHtml = yahooResponse.getContentText();
      
      // Extract Forward Dividend using regex
      const yahooDividendMatch = yahooHtml.match(/Forward Dividend.*?(\d+\.\d+)/);
      const forwardDividend = yahooDividendMatch ? yahooDividendMatch[1] : "N/A";
      
      // Insert into your specified cell (e.g., Sheet1!B2)
      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
      sheet.getRange("B2").setValue(forwardDividend);
    }
    
  3. Save the script, click the run button, and grant the necessary permissions when prompted.
  4. For auto-updates, set up a time-driven trigger (under Edit > Current project's triggers) to run the script daily or weekly.

Inserting to a Specific Field

Whether you use IMPORTXML or Apps Script, just target the exact cell (e.g., Sheet1!D5) where you want the 0.82 value to go—either by entering the formula directly in that cell, or specifying the cell range in your Apps Script code.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 11:37:45