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

如何从网页提取表格?已编写Python代码但无法找到目标表格

Hey there! Let's work through why you're not able to locate that target table. Here are some actionable steps to fix this:

1. Add a User-Agent Header to Avoid Being Blocked

Many websites flag default requests library requests as bots and block them. Adding a realistic user-agent header can help you get the full page content:

import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
}
page = requests.get('http://www.moneycontrol.com/financials/nmdc/ratios/NMD02', headers=headers)
soup = BeautifulSoup(page.text, 'html.parser')

2. Verify the Table's Actual Selector

Class names can change, or the table might be nested in unexpected elements. Instead of relying on the assumed class, list all tables on the page to identify the correct one:

tables = soup.find_all('table')
for idx, table in enumerate(tables):
    print(f"Table {idx} classes: {table.get('class')}")
    # Optional: Print a snippet of the table to check content
    print(table.text[:200] + "\n---")

Once you find the table that matches your target content, use its specific class or index to fetch it (e.g., tables[2] if it's the third table).

3. Check if the Table Loads Dynamically

If the table is rendered via JavaScript after the initial page load, requests won't capture it (since it only fetches static HTML). In that case, use a tool that renders JavaScript like Selenium:

from selenium import webdriver
from bs4 import BeautifulSoup

# Make sure ChromeDriver is installed and in your system PATH
driver = webdriver.Chrome()
driver.get('http://www.moneycontrol.com/financials/nmdc/ratios/NMD02')

# Wait a second for dynamic content to load (adjust time if needed)
driver.implicitly_wait(1)

soup = BeautifulSoup(driver.page_source, 'html.parser')
table = soup.find(class_='tabns MR10')  # Or the correct selector you found
driver.quit()

Start with the user-agent fix first—it's the most common issue here. If that doesn't work, inspect the table list to find the right selector, and fall back to Selenium if dynamic content is the problem.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:38:01