首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么无法获取指定url的带curl的重定向url?

为什么无法获取指定url的带curl的重定向url?
EN

Stack Overflow用户
提问于 2019-04-25 15:56:14
回答 1查看 164关注 0票数 1

在浏览器中打开url http://quotes.money.163.com/f10/gszl_600024.html并等待5秒钟左右,url将重定向到http://quotes.money.163.com/stock中。在一些关于curl命令的教程中,L指令告诉cURL遵循重定向,s指令告诉cURL保持沉默,url_effective变量是我们想要的。

代码语言:javascript
复制
target="http://quotes.money.163.com/f10/gszl_600024.html"
curl -Ls -w %{url_effective} -o /dev/null $target

为什么上面的命令无法获取最后一个重定向的url http://quotes.money.163.com/stock

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-25 16:42:56

因为它是一个HTML meta tag redirect,而curl不支持自动跟随HTML meta标签重定向。要做到这一点,您需要一些能够理解HTML的东西,而curl则不需要。

quotes.money.163.com/f10/gszl_600024.html包含html标记<meta http-equiv="refresh" content="5; url=/">,它告诉浏览器after 5 seconds, redirect to the root of this domain哪个是quotes.money.163.com/,而quotes.money.163.com/反过来从http://img1.cache.netease.com/f2e/finance/backend_project/quotes_index_2014/app/dist/js/quotes_hub.916069.min.js加载javascript,它包含

代码语言:javascript
复制
! function(o) {
    var l = location.href,
        t = !!location.hash.split("#")[1] ? location.hash.split("#")[1] : "HS",
        c = location.host,
        n = location.protocol,
        i = {
            HS: "stock",
            US: "usstock",
            HK: "hkstock",
            BOND: "bond",
            FX: "old/#FX",
            FN: "old/#FN",
            FU: "old/#FU",
            GB: "old/#GB",
            DC: "old/#DC"
        },
        e = i[t],
        a = n + "//" + c + "/stock";

    function s() {
        return l.indexOf("quotes.money.163.com/old/") > -1 ? 1 : 0
    }

    function r(o) {
        return o.indexOf("query") > -1 ? 1 : 0
    }
    if (!s()) {
        if (e) {
            location.replace(n + "//" + c + "/" + e)
        } else {
            if (r(t)) {
                location.replace(n + "//" + c + "/old/#" + t)
            } else {
                location.replace(a)
            }
        }
    }
}(this);

它通过修改window.location执行到http://quotes.money.163.com/stock的javascript重定向,更糟糕的是,curl既不理解javascript重定向,也不理解html重定向。如果你想要能同时理解这两种语言的东西,考虑使用无头浏览器。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55844427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档