Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xpath - Google Sheet use Importxml error could not fetch url

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/item/Pack_of_sugar","//*[@id='__layout']/div/div[1]/div/div[4]/div[1]/div[2]/div[1]/div[2]")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • 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:

enter image description here

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...