我希望获得http://www.images.watoday.com.au/business/markets/movers
中的数据以及#34;整个市场最佳收视者"仅
我的代码如下:
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'....]
答案 0 :(得分:1)
如何限制文字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`