Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IMPORTXML not selecting first element

I've spent the past couple of days trying to figure out how to only get the first element using the following XMLIMPORT.

This is the URL of the website I'm trying to scrape: https://www.grainger.com/product/DURACELL-Battery-5LE23

I'm trying to get the price $25.45 without getting all the other prices ($17.36 $6.97, $32.80, $7.29, $19.09, $24.34, $149.05).

I've been using: =IMPORTXML("https://www.grainger.com/product/DURACELL-Battery-5LE23", "//span[@itemprop='price']/text()[1]") and it keeps showing me all 8 prices.

What am I doing wrong?

like image 405
Fia Avatar asked Oct 17 '25 05:10

Fia


2 Answers

Looks that the referred web page source was change. At this time the following XPath query

//span[@class = 'gcprice-value']

which returns a similar result:

      $13.49
      $12.14
      $34.60
      $30.50
       $4.52
      $13.49
       $6.07
     $160.50

In order to get only the first price, there are several alternatives:

  1. use the normalize-space function:
normalize-space(//*/span[@class = 'gcprice-value'])
  1. Nest IMPORTXML inside another Google Sheets function:
    (assume that A1 and A2 holds the URL, and XPath query respectively
=Index(IMPORTXML(A1, A2),1)
  1. Use another XPath like the one provided by Chrome Developers tools
//*[@id="addItemsToCartFromIdp"]/div[1]/div[1]/div/div[1]/p/span[2]
like image 165
Rubén Avatar answered Oct 21 '25 03:10

Rubén


You could wrap your function in a split formula to split on the new line character which effectively just ends up excluding the rest of the data:

=SPLIT(IMPORTXML("http://www.grainger.com/product/DURACELL-Battery-5LE23", "//span[@class='gcprice-value']"),CHAR(10))

enter image description here

like image 23
Aurielle Perlmann Avatar answered Oct 21 '25 04:10

Aurielle Perlmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!