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

使用BeautifulSoup4与Python提取HTML悬浮提示中的表格数字

Fixing the Hidden Hover Data Extraction with BeautifulSoup

Hey Marco, I’ve run into this exact issue with CoinGecko’s hover tooltips before—those numbers are tucked away in an HTML string inside an attribute, not directly in the page’s DOM. Let’s break down how to pull that 629925 value (and others like it) properly.

Why Your Current Code Isn’t Working

The hover details you’re after are stored in the data-original-title attribute of the .percent divs. This attribute contains a full snippet of HTML (like a table with <td> elements), but when you call developer[0].findAll("td"), BeautifulSoup is looking for <td> elements that are children of the .percent div—not the HTML string inside the attribute. That’s why you’re getting empty lists!

Step-by-Step Solution

Here’s how to extract that nested HTML data:

  1. Fetch and parse the main page correctly (I spotted a small typo in your code—you used fp instead of response.text)
  2. Pull the data-original-title attribute from each .percent element
  3. Treat the attribute’s value as a new HTML document and parse it with BeautifulSoup
  4. Extract the target number from this parsed tooltip HTML

Working Code Example

from bs4 import BeautifulSoup as soup
import requests

url = "https://www.coingecko.com/de?page=1"
response = requests.get(url)
# Parse the main page HTML
webpage = soup(response.text, "html.parser")

# Get all .percent divs containing hover tooltips
developer_sections = webpage.findAll("div", {"class": "percent"})

for section in developer_sections:
    # Extract the tooltip's HTML string from the attribute
    tooltip_html = section.get("data-original-title")
    if tooltip_html:  # Skip elements that don't have hover data
        # Parse the tooltip HTML as a separate soup object
        tooltip_soup = soup(tooltip_html, "html.parser")
        # Extract the target number (adjust index if your number is in a different <td>)
        target_number = tooltip_soup.find("td").text.strip()
        print(f"Extracted hover number: {target_number}")

Quick Adjustments to Note

  • If your target number isn’t in the first <td> of the tooltip table, use tooltip_soup.findAll("td")[index] (replace index with the position of your desired cell, e.g., [1] for the second cell)
  • You can swap html.parser with lxml if you have it installed—just update both soup initialization lines

This approach works because we’re treating the attribute’s HTML string as a standalone document, which lets us access the <td> elements hidden inside the hover tooltip content.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:34:49