0

次の 2 つのページから価格情報を抽出しようとしています。

http://jujumarts.com/mobiles-accessories-smartphones-wildfire-sdarkgrey-p-551.html http://jujumarts.com/computers-accessories-transcend-500gb-portable-storejet-25d2-p-2616.html

xpath1 = //span[@class='productSpecialPrice']//text()
xpath2 = //div[@class='proDetPrice']//text()

今のところ、成功した場合は xpath1 の結果を返し、それ以外の場合は 2 番目のコードを実行する Python コードを作成しました。このロジックを xpath だけで実装することは可能だと思いますが、誰か教えてもらえますか?

4

1 に答える 1

4

|以下を示すために使用しますunion

xpath3 = "//span[@class='productSpecialPrice']//text()|//div[@class='proDetPrice']//text()"

これはまさにあなたが求めたものではありませんが、実行可能なソリューションに組み込むことができると思います.


XPath (バージョン 1.0) 仕様から:

| | 演算子は、そのオペランドの和集合を計算します。これは、ノード セットでなければなりません。


例えば、

import lxml.html as LH

urls = [
    'http://jujumarts.com/mobiles-accessories-smartphones-wildfire-sdarkgrey-p-551.html',
    'http://jujumarts.com/computers-accessories-transcend-500gb-portable-storejet-25d2-p-2616.html'
    ]

xpaths = [
    "//span[@class='productSpecialPrice']//text()",
    "//div[@class='proDetPrice']//text()",
    "//span[@class='productSpecialPrice']//text()|//div[@class='proDetPrice']//text()"
    ]
for url in urls:
    doc = LH.parse(url)
    for xpath in xpaths:
        print(doc.xpath(xpath))
    print

収量

['Rs.11,800.00']
['Rs.13,299.00', 'Rs.11,800.00']
['Rs.13,299.00', 'Rs.11,800.00']

[]
['Rs.7,000.00']
['Rs.7,000.00']

必要な情報を得るもう 1 つの方法は、

"//*[@class='productSpecialPrice' or @class='proDetPrice']//text()" 
于 2013-04-23T12:49:52.133 に答える