Skip to main content
 首页 » 编程设计

Python:xpath 稳健地获取表中的数据

2025年01月19日9kenshinobiy

我只想在 http://www.images.watoday.com.au/business/markets/movers 中获取“WHOLE MARKET TOP GAINERS”的数据。

我的代码如下:

import requests 
from lxml import html 
 
page_gain = requests.get('http://www.images.watoday.com.au/business/markets/movers') 
gain = html.fromstring(page_gain.content) 
name = gain.xpath('//h2[contains(.,"Whole Market Top Gainers")]/following::a/text()') 
data = gain.xpath('//h2[contains(.,"Whole Market Top Gainers")]/following::td/text()') 

我想要的输出是

['MEM','MEMPHASYS LTD','0.002','0.001','100.00','1,000,000','AUH','AUSTCHINA HOLDINGS','0.007','0.002','40.00','1,433,311'....]  

请您参考如下方法:

如何限制第一个表格(following::table[1])中文本Whole Market Top Gainers 之后的行:

>>> gain = ... 
>>> expr = ('//h2[contains(.,"Whole Market Top Gainers")]' 
            '/following::table[1]/tbody/tr') 
>>> rows = gain.xpath() 
>>> [[td.text_content().strip() for td in row] for row in rows] 
[['AJC', 'ACACIA COAL LTD', '0.002', '0.001rise', '100.00rise', '92,525'], 
 ['MEM', 'MEMPHASYS LTD', '0.002', '0.001rise', '100.00rise', '1,000,000'], 
 ['AUH', 'AUSTCHINA HOLDINGS', '0.007', '0.002rise', '40.00rise', '1,433,311'], 
 ['AO1', 'ASSETOWL LIMITED', '0.100', '0.025rise', '33.33rise', '249,180'], 
 ['BAS', 'BASS OIL LTD', '0.004', '0.001rise', '33.33rise', '15,390,472'], 
 ['RNL', 'RISION LIMITED', '0.004', '0.001rise', '33.33rise', '6,100,812'], 
 ['PAB', 'PATRYS LIMITED', '0.061', '0.013rise', '27.08rise', '86,337,514'], 
 ['IQ3', 'IQ3CORP LIMITED', '0.250', '0.050rise', '25.00rise', '6,000'], 
 ['SMA', 'SMARTTRANS HOLDINGS', '0.005', '0.001rise', '25.00rise', '70,000'], 
 ['SEI', 'SPECIALITY METALINT', '0.035', '0.006rise', '20.69rise', '12,162,844']] 
# td.text_content().strip().replace('rise', '') to remove `rise`