2016-11-08 38 views
0
<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'? 我想直接指向市盈率。python:xpath lxml提取数据

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()') 

也导致[]

+2

你试过写点什么吗? – Dekel

+0

这是一个重复的http://stackoverflow.com/questions/40488422/python-get-data-from-changing-span-class-using-lxml-xpath – Markus

+0

你错过了一个'span'。 – Markus

回答

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 
+0

谢谢。有用!你可以添加如何使用contains()? – vindex

+0

@vindex答案已更新 – Markus