Google表格使用Importxml错误无法获取网址 [英] Google Sheet use Importxml error could not fetch url
问题描述
我想在此网站上获取价格数据( https://tarkov-market.com/item /Pack_of_sugar )
I want to get price data on this website (https://tarkov-market.com/item/Pack_of_sugar)
但这不起作用
=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]")
推荐答案
- 您要从
https://tarkov-market.com/item/Pack_of_sugar
的网址中检索类似55,500₽
的价格,并将其放入Google Spreadsheet上的单元格中. - You want to retrieve the price like
55,500₽
from the URL ofhttps://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?
不幸的是,IMPORTXML
不能用于这种情况.因为IMPORTXML
的用法与=IMPORTXML("https://tarkov-market.com/item/Pack_of_sugar","//*")
相似,所以会出现无法从URL检索值之类的错误.因此,在这种情况下,作为一种解决方法,我想建议使用Google Apps脚本作为自定义函数.使用Google Apps脚本时,可以检索该值.
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.
请复制以下脚本并将其粘贴到电子表格的容器绑定脚本中.并将=sampleFormula()
放到单元格中.这样,可以将值放到单元格中.
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:
- 该脚本供您提问.因此,当将此脚本用于其他URL和场景时,可能会发生错误.请注意这一点.
- Custom Functions in Google Sheets
- Class UrlFetchApp
这篇关于Google表格使用Importxml错误无法获取网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!