2013-03-02 3 views
1

나는 치료를 시도하고있다.파이썬/Xpath로 변환 쿼리

hxs.select('//span[contains(@itemprop, "price")]').extract() 

출력 : 나는 다음있어, 즉

16.95 

분수 가격 범위 교체 +와 가격을 추가

[u'<span itemprop="price" class="offer_price">\n<span class="currency">\u20ac</span>\n16<span class="offer_price_fraction">,95</span>\n</span>'] 

가 어떻게이 출력을 검색 할 수 있습니다 , 함께. 여기

답변

1

사용이 하나의 XPath 식 :

translate(
      concat(//span[@itemprop = 'price']/text()[normalize-space()], 
        //span[@itemprop = 'price']/span[@class='offer_price_fraction'] 
        ), 
      ',', 
      '.' 
      ) 

XSLT 기반의 rification :이 변환이 XML 문서에 적용

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="/"> 
    <xsl:copy-of select= 
    "translate(
      concat(//span[@itemprop = 'price']/text()[normalize-space()], 
        //span[@itemprop = 'price']/span[@class='offer_price_fraction'] 
       ), 
      ',', 
      '.' 
      )"/> 
</xsl:template> 
</xsl:stylesheet> 

: XPath 식을 평가하고,이 평가 결과가 출력 복사

<span itemprop="price" class="offer_price"> 
    <span class="currency">\u20ac</span> 
16<span class="offer_price_fraction">,95</span> 
</span> 

:

16.95 
+0

와우, 굉장해. 감사!! –

+0

@MauriceKroon, 여러분은 환영합니다. –

1

내가 XPath를 선택기 설정이 방법은 다음과 같습니다

>>> hxs.extract() 
u'<html><body><span itemprop="price" class="offer_price">\n<span class="currency">\u20ac</span>\n16<span class="offer_price_fraction">,95</span>\n</span></body></html>' 

을 여기에 당신이 원하는 결과를 달성 할 수있는 방법입니다

>>> price = 'descendant::span[@itemprop="price"]' 
>>> whole = 'text()' 
>>> fract = 'descendant::span[@class="offer_price_fraction"]/text()' 
>>> s = hxs.select(price).select('%s | %s' % (whole, fract)).extract() 
>>> s 
[u'\n', u'\n16', u',95', u'\n'] 
>>> ''.join(s).strip().replace(',', '.') 
u'16.95' 
+0

감사합니다. 자손에 대해 잊어, 바보 같은 .. 감사합니다! –