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

Google sheets IMPORTXML and XPath href and title not returned


I need to extract some URLS and their respective titles using Google Sheets ImportXML with Xpath.

I found these previous answers and tested them but they don't seem to be working for my case:

Extract URL using xpath

How do I return a specific link address using IMPORTXML and XPath

I tested these formulas:

=IMPORTXML("https://ok.ru/video/334044074277","//*[contains(@class, 'video-card_n-w')]//a/@href", "en_US")

=IMPORTXML("https://ok.ru/video/334044074277","//*[contains(@class, 'video-card_n-w')]/a/@title", "en_US")

href returns #.

The xpaths seems correct on chrome developer tool.

EDIT1: title works now but not href.

EDIT2: I tested this also but same # result: =IMPORTXML("https://ok.ru/video/334044074277","//*[contains(@class, 'video-card_n ellip')]/@href", "en_US").


Solution

  • if you run:

    =IMPORTXML("https://ok.ru/video/c1458725","//@href")
    

    enter image description here

    you can see why are you getting #


    try:

    =INDEX(LET(x, "https://ok.ru", HYPERLINK(x&QUERY(
     IMPORTXML(x&"/video/c1458725","//@href", "en_US"), "where Col1 matches '/video.*album'", ), 
     IMPORTXML(x&"/video/c1458725","//*[contains(@class, 'video-card_n ellip')]"))))
    

    enter image description here