2
votes

I tried to webscrape the content title of post in this website : https://www.hortidaily.com/sector/553/greenhouse/

using Google Sheets Importxml function.

All posts are written in the same format :

<a class="article" href="link1">
 <img src="img1.jpg" align="default" border="0" class="indexdefault">      
    <h1>Titre1</h1>
    <p>Texte1</p>
</a>

ImportXML function with Xpath = :

- //a[@class ='article']/@href return link1 : ok
- //a[@class ='article'][1]/img/@src return img1 : ok
- //a[@class ='article'][1]/h1 return #NA (imported content is empty) eventhough the XPath is working in chrome XPath Helper...

Although //h1[1] return the title of the first article, the problem I do not understand why //a[@class ='article'][1]/h1 doesn't work and I want to be sure the h1 I get is the h1 under the first <a class="article" href="link1">

I have tried almost everything I could imagine, do not find the issue. I need some help !

Thanking you in advance

1

1 Answers

0
votes
  • You want to retrieve the value of h1 in the 1st a[@class ='article'].
  • You want to know that why //a[@class ='article'][1]/h1 returns #NA.

If my understanding is correct how about this answer?

Reason of issue:

<div id="hoofdartikelen">

  <a class="article" href="link1">
    <img src="img1.jpg" align="default" border="0" class="indexdefault">
    <h1>Titre1</h1>
    <p>Texte1</p>
  </a>

</div>

In my investigation, for above HTML data, it seems that the tag name a of <a class="article" href="link1"> is the issue. For example, when the tag name is modified to div, it could confirmed that =IMPORTXML(A1,"//div[@class ='article'][1]/h1") worked.

And also, it seems that at above HTML, the tags h1 and p are not the children of the tag a. So the following formulas work.

=IMPORTXML(A1,"//div[@id='hoofdartikelen']/h1[1]")

=IMPORTXML(A1,"//div[@id='hoofdartikelen']/p[1]")

But, =IMPORTXML(A1,"//div[@id='hoofdartikelen']/img[1]/@src") doesn't work. It is required to be =IMPORTXML(A1,"//div[@id='hoofdartikelen']/a[1]/img/@src"). So it seems that the tag img is the child of a.

xpath samples:

From above results, in your case, I think that the following xpath samples might be suitable.

  • //div[@id='hoofdartikelen']/h1[1]
  • //h1[1]
  • //a[@class ='article']/../h1[1]

Reference: