python:xpath lxml来提取数据

时间:2016-11-08 15:43:33

标签: python xpath lxml

<td> <span class="data_lbl updated-daily">P/E Ratio <small class="data_meta">(including extraordinary items)</small></span> <span class="data_data"> <span class="marketDelta deltaType-negative">-69.83</span> </span> </td>

如何以健壮的方式提取数据PE比率数据'-69.83'? 我想直接指出市盈率。

from lxml import html
import requests

StockData =['AASIA']
page_wsj1 = requests.get('http://quotes.wsj.com/MY/'+StockData[x]+'/financials')
wsj1 = html.fromstring(page_wsj1.content)
PE = wsj1.xpath('//td[contains(.,"P/E Ratio")]/text()')

但结果是['','','','','']

wsj1.xpath('//td[normalize-space(span) = "P/E Ratio"]/span[@class = "data_data"]/span/text()')

也是结果[]

1 个答案:

答案 0 :(得分:0)

//td[normalize-space(span/text()) = "P/E Ratio"]/span[@class = "data_data"]/span

//td[contains(normalize-space(span), "P/E Ratio")]/span[@class = "data_data"]/span