Return link 重定向 url
Return link to redirect url
我正在尝试获取特定网站的最终目的地的 url,但我发现在我的电子表格中用作函数的所有模板,只有 return初始 link:
function getRedirect(url) {
var response = UrlFetchApp.fetch(url, {'followRedirects': false, 'muteHttpExceptions': false});
var redirectUrl = response.getHeaders()['Location']; // undefined if no redirect, so...
var responseCode = response.getResponseCode();
if (redirectUrl) { // ...if redirected...
var nextRedirectUrl = getRedirect(redirectUrl); // ...it calls itself recursively...
Logger.log(url + " is redirecting to " + redirectUrl + ". (" + responseCode + ")");
return nextRedirectUrl;
}
else { // ...until it's not
Logger.log(url + " is canonical. (" + responseCode + ")");
return url;
}
}
这是我放的模型:
=getRedirect("https://c.newsnow.co.uk/A/1067471289?-833:12")
在电子表格中 returns:
https://c.newsnow.co.uk/A/1067471289?-833:12
我想在重定向后收集 link 到:
https://sports.ndtv.com/football/europa-league-bruno-fernandes-double-helps-manchester-united-thrash-real-sociedad-gareth-bale-stars-for-tottenham-2373767
当我看到URLhttps://c.newsnow.co.uk/A/1067471289?-833:12
的HTML时,我想在这种情况下,https://sports.ndtv.com/football/europa-league-bruno-fernandes-double-helps-manchester-united-thrash-real-sociedad-gareth-bale-stars-for-tottenham-2373767
的值可能可以直接使用IMPORTXML
和一个 xpath。示例公式如下
示例公式:
=IMPORTXML(A1,"//a/@href")
- 这种情况下,请将
https://c.newsnow.co.uk/A/1067471289?-833:12
的URL填入“A1”单元格。
结果:
使用 Google Apps 脚本:
当你想使用GoogleApps脚本时,你也可以使用下面的脚本。在这种情况下,请将 =SAMPLE("https://c.newsnow.co.uk/A/1067471289?-833:12")
的自定义公式放入单元格。
function SAMPLE(url) {
const res = UrlFetchApp.fetch(url).getContentText();
const v = res.match(/url: '([\s\S\w]+?)'/);
return v && v.length == 2 ? v[1].trim() : "Value cannot be retrieved.";
}
注:
- 在此示例公式中,xpath 用于
https://c.newsnow.co.uk/A/1067471289?-833:12
的 URL。所以当你把它用于其他URL时,它可能无法使用。所以请注意这一点。
参考:
我正在尝试获取特定网站的最终目的地的 url,但我发现在我的电子表格中用作函数的所有模板,只有 return初始 link:
function getRedirect(url) {
var response = UrlFetchApp.fetch(url, {'followRedirects': false, 'muteHttpExceptions': false});
var redirectUrl = response.getHeaders()['Location']; // undefined if no redirect, so...
var responseCode = response.getResponseCode();
if (redirectUrl) { // ...if redirected...
var nextRedirectUrl = getRedirect(redirectUrl); // ...it calls itself recursively...
Logger.log(url + " is redirecting to " + redirectUrl + ". (" + responseCode + ")");
return nextRedirectUrl;
}
else { // ...until it's not
Logger.log(url + " is canonical. (" + responseCode + ")");
return url;
}
}
这是我放的模型:
=getRedirect("https://c.newsnow.co.uk/A/1067471289?-833:12")
在电子表格中 returns:
https://c.newsnow.co.uk/A/1067471289?-833:12
我想在重定向后收集 link 到:
https://sports.ndtv.com/football/europa-league-bruno-fernandes-double-helps-manchester-united-thrash-real-sociedad-gareth-bale-stars-for-tottenham-2373767
当我看到URLhttps://c.newsnow.co.uk/A/1067471289?-833:12
的HTML时,我想在这种情况下,https://sports.ndtv.com/football/europa-league-bruno-fernandes-double-helps-manchester-united-thrash-real-sociedad-gareth-bale-stars-for-tottenham-2373767
的值可能可以直接使用IMPORTXML
和一个 xpath。示例公式如下
示例公式:
=IMPORTXML(A1,"//a/@href")
- 这种情况下,请将
https://c.newsnow.co.uk/A/1067471289?-833:12
的URL填入“A1”单元格。
结果:
使用 Google Apps 脚本:
当你想使用GoogleApps脚本时,你也可以使用下面的脚本。在这种情况下,请将 =SAMPLE("https://c.newsnow.co.uk/A/1067471289?-833:12")
的自定义公式放入单元格。
function SAMPLE(url) {
const res = UrlFetchApp.fetch(url).getContentText();
const v = res.match(/url: '([\s\S\w]+?)'/);
return v && v.length == 2 ? v[1].trim() : "Value cannot be retrieved.";
}
注:
- 在此示例公式中,xpath 用于
https://c.newsnow.co.uk/A/1067471289?-833:12
的 URL。所以当你把它用于其他URL时,它可能无法使用。所以请注意这一点。