Google Sheet use Importxml error could not fetch url

后端 未结 1 1151
花落未央
花落未央 2020-12-17 06:51

I want to get price data on this website (https://tarkov-market.com/item/Pack_of_sugar)

But it doesn\'t work

=IMPORTXML(\"https://tarkov-market.com/i         


        
相关标签:
1条回答
  • 2020-12-17 07:18
    • You want to retrieve the price like 55,500₽ from the URL of https://tarkov-market.com/item/Pack_of_sugar and put to a cell on Google Spreadsheet.

    I could understand like this. If my understanding is correct, how about this answer?

    Issue and workaround:

    Unfortunately, IMPORTXML cannot be used for this situation. Because IMPORTXML is used like =IMPORTXML("https://tarkov-market.com/item/Pack_of_sugar","//*"), an error like the value cannot be retrieved from the URL occurs. So in this case, as a workaround, I would like to propose to use Google Apps Script as a custom function. When Google Apps Script is used, the value can be retrieved.

    Sample script:

    Please copy and paste the following script to the container-bound script of the Spreadsheet. And please put =sampleFormula() to a cell. By this, the value can be put to the cell.

    function sampleFormula() {
      const url = "https://tarkov-market.com/item/Pack_of_sugar";
      const html = UrlFetchApp.fetch(url).getContentText();
      return html.match(/price:(.+?)<\/title>/)[1].trim();
    }
    
    Result:

    Note:

    • This script is for your question. So when this script is used for other URL and scenes, an error might occur. Please be careful this.

    References:

    • Custom Functions in Google Sheets
    • Class UrlFetchApp
    0 讨论(0)
提交回复
热议问题