2016-11-08 39 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