我試圖通過使用XPath的Google Spreadsheet中的importXML從this site中獲取元素屬性的值。Phantom <span>元素在Google電子表格中使用帶XPath的ImportXML
我尋找的屬性值是content
,在<span>
中找到,itemprop="price"
。
<div class="left" style="margin-top: 10px;">
<meta itemprop="currency" content="RON">
<span class="pret" itemprop="price" content="698,31 RON">
<p class="pret">Pretul tau:</p>
698,31 RON
</span>
...
</div>
我可以訪問<div class="left">
但我不能獲取到<span>
元素。
嘗試使用:
//span[@class='pret']/@content
我得到#N/A;//span[@itemprop='price']/@content
我得到了#N/A;//div[@class='left']/span[@class='pret' and @itemprop='price']/@content
我得到#N/A;//div[@class='left']/span[1]/@content
我得到#N/A;//div[@class='left']/span/text()
得到文本節點<span>
我得到#N/A;//div[@class='left']
//
span/text()
我得到了div.left
一個<span>
的文本節點下。
要得到文本節點<span>
我必須使用//div[@class='left']/text()
。但是我不能使用該文本節點,因爲如果產品正在銷售中,跨度的佈局會發生變化,所以我需要該屬性。
這就像我尋找的跨度不存在,雖然它出現在Chrome的開發視圖中,並且在頁面源代碼中和使用$x("")
的控制檯中的所有XPath工作中。
我試圖直接通過右鍵單擊生成XPath的開發工具,我得到//*[@id='produs']/div[4]/div[4]/div[1]/span
哪些不起作用。我也試圖用Firefox生成XPath,FF和Chrome的插件無濟於事。以這些方式生成的XPath甚至在我用「手動編碼的XPath」設法掃描的站點上都不起作用。
現在,最奇怪的是,在這個other site與代碼結構明顯相似的XPath //span[@itemprop='price']/@content
的作品。
我現在掙扎了4天。我開始認爲這與自動關閉元標記有關,但爲什麼不在另一個網站上發生?
感謝您的答案,但我需要span的屬性值,而不是文本節點。我不知道索引函數,它是非常有用的,但遺憾的是不是在這種情況下。 –
@MacoveiVlad也許最新的答案更新,可以幫助任何事情。 – wchiquito
非常感謝你爲myImportXml定製的功能,現在爲我解決了一個問題! –