I am trying to get the value of an element attribute from this site via importXML in Google Spreadsheet using XPath.
The attribute value i seek is content
found in the <span>
with 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>
I can access <div class="left">
but i can't get to the <span>
element.
Tried using:
//span[@class='pret']/@content
i get #N/A;//span[@itemprop='price']/@content
i get #N/A;//div[@class='left']/span[@class='pret' and @itemprop='price']/@content
i get #N/A;//div[@class='left']/span[1]/@content
i get #N/A;//div[@class='left']/span/text()
to get the text node of<span>
i get #N/A;//div[@class='left']
//
span/text()
i get the text node of a<span>
lower indiv.left
.
To get the text node of <span>
i have to use //div[@class='left']/text()
. But i can't use that text node because the layout of the span changes if a product is on sale, so i need the attribute.
It's like the span i'm looking for does not exist, although it appears in the development view of Chrome and in the page source and all XPath work in the console using $x("")
.
I tried to generate the XPath directly form the development tool by right clicking and i get //*[@id='produs']/div[4]/div[4]/div[1]/span
which does not work. I also tried to generate the XPath with Firefox and plugins for FF and Chrome to no avail. The XPath generated in these ways did not even work on sites i managed to scrape with "hand coded XPath".
Now, the strangest thing is that on this other site with apparently similar code structure the XPath //span[@itemprop='price']/@content
works.
I struggled with this for 4 days now. I'm starting to think it's something to do with the auto-closing meta tag, but why doesn't this happen on the other site?
Best Answer
Perhaps the following formulas can help you:
Or
UPDATE
It seems that not properly parse the entire document, it fails. A document extraction, something like:
works with the following XPath query:
UPDATE
It occurs to me that one option is that you can use Apps Script to create your own ImportXML function, something like:
Then you can use as follows: