我尝试检查某些网站的 html,但内容存在重定向。 如何从html中获取重定向的URL?
注意:使用这些代码,我们可以重定向页面:
<meta http-equiv="refresh" content="0; url=http://example.com/" />
或
<script type="text/javascript">
window.location.href = "http://example.com"
</script>
或
<link rel="canonical" href="http://www.example.com/product.php?item=swedish-fish"/>
或
<meta http-equiv="Refresh" content="3; url=http://example.com/alternate_url.html">
或
<noscript>
<meta http-equiv="refresh" content="3" />
</noscript>
...
最佳答案
添加id
属性到元标记,如下所示:
<meta id="idRefresh" http-equiv="Refresh" content="5555555; url=http://example.com/alternate_url.html"/>
然后直接使用 Javascript
var e = document.querySelector("meta[id='idRefresh']");
var content = e && e.getAttribute("content");
var result = content && content.replace("5555555; url=","");
console.log(result);
//results in
//http://example.com/alternate_url.html
注意:如果您更改刷新时间(在我的测试示例中为 5555555),那么您还需要在 replace(..)
中更改它。方法
关于javascript - 在 javascript 或 c# 中从 Html 源查找重定向 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60151134/