Search code examples
google-sheetsweb-scrapingxpathgoogle-sheets-formula

xpath - Import div by id


Using importxml/ xpath in Google spreadsheet I can import a html span element with a certain id:

=importxml("https://stackoverflow.com"; "//span[@id='svnrev']")

What is the xpath code to find a div with a certain id? I tried:

=importxml("https://stackoverflow.com"; "//div[@id='interestingTags']")
=importxml("https://stackoverflow.com"; "//div[@id='interestingTags']/a")
=importxml("https://stackoverflow.com"; "//*[@id='interestingTags']")

But I always get an error (import is empty). That's the HTML I'm trying to import:

<div id="interestingTags"><a href="/questions/tagged/adwords-script" class="post-tag" title="show questions tagged &#39;adwords-script&#39;" rel="tag">adwords-script</a> </div>
<span id="svnrev">rev 2017.8.31.26908</span>

Update: What I want to see as a result is either

<a href="/questions/tagged/adwords-script" class="post-tag" title="show questions tagged &#39;adwords-script&#39;" rel="tag">adwords-script</a> 

... or just: adwords-script


Solution

  • Problem solved: The div I tried to target was dynamic content that was only shown in this session.

    Usually, the code works fine. Here another example that works:

    =importxml("https://www.w3schools.com/xml/xpath_syntax.asp"; "//*[@id='nav_examples']")