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

Python中如何通过稳健代码获取路透社指定页面的EPS Normalized (Annual) 1,010.14数据

How to Robustly Extract EPS Normalized (Annual) Value Using XPath

Let's break down why your current XPath isn't working, then fix it with a targeted, reliable approach.

The Issue with Your Current XPath

Your expression //td/span/text() is way too broad—it grabs every <span> inside every <td> on the page, which is why you're getting that long list of unrelated numbers. We need to narrow things down to specifically the row containing the "EPS Normalized (Annual)" label.

Targeted XPath Strategy

From your element inspection screenshot, the value "1,010.14" sits in the <td> directly to the right of the <td> that holds the "EPS Normalized (Annual)" label. Here's a precise XPath to target exactly that:

//td[normalize-space(text())='EPS Normalized (Annual)']/following-sibling::td[1]/span/text()
  • normalize-space(text())='EPS Normalized (Annual)': Ensures we match the exact label, ignoring any extra whitespace that might be present in the HTML
  • following-sibling::td[1]: Grabs the very next <td> element (the one containing our target value)
  • /span/text(): Extracts the text inside the span within that target td

Robust Code Implementation

Also, note that Reuters often blocks raw requests calls without proper headers. Let's add a user-agent to mimic a browser request, plus basic error handling for edge cases:

from lxml import html
import requests

# Add headers to avoid being blocked by Reuters' anti-scraping measures
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}

try:
    page = requests.get('https://www.reuters.com/markets/companies/AALI.JK/key-metrics/per-share-data', headers=headers)
    page.raise_for_status()  # Trigger error if request fails (e.g., 404, 500)
    
    data = html.fromstring(page.content)
    
    # Use the targeted XPath to fetch the value
    eps_normalized = data.xpath("//td[normalize-space(text())='EPS Normalized (Annual)']/following-sibling::td[1]/span/text()")
    
    if eps_normalized:
        print(f"EPS Normalized (Annual): {eps_normalized[0]}")
    else:
        print("Could not locate the EPS Normalized (Annual) value—page structure may have changed.")
except Exception as e:
    print(f"An error occurred: {str(e)}")

Why This Works

  • The headers help bypass basic anti-scraping checks that would block a raw requests call
  • The XPath directly targets the row you care about, so you won't pull in unrelated data
  • Error handling ensures you get clear feedback if the request fails or the value can't be found (in case the page structure changes later)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 00:24:08