Python中如何通过稳健代码获取路透社指定页面的EPS Normalized (Annual) 1,010.14数据
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 HTMLfollowing-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
requestscall - 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

