【问题标题】:Why can't get redirect url with curl for the specified url?为什么无法使用 curl 获取指定 url 的重定向 url?
【发布时间】:2019-04-25 07:56:14
【问题描述】:

在浏览器中打开网址http://quotes.money.163.com/f10/gszl_600024.html,等待5秒左右,网址会重定向到http://quotes.money.163.com/stock。 在 curl 命令的一些教程中,L 指令告诉 cURL 跟随重定向,s 指令告诉 cURL 保持沉默,url_effective 变量是我们所追求的。

target="http://quotes.money.163.com/f10/gszl_600024.html"
curl -Ls -w %{url_effective} -o /dev/null $target

为什么上面的命令获取不到最后一个重定向的urlhttp://quotes.money.163.com/stock

【问题讨论】:

    标签: curl url-redirection


    【解决方案1】:

    因为它是HTML meta tag redirect,并且 curl 不支持自动跟随 HTML 元标记重定向。要遵循你需要理解 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 domainquotes.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,其中包含

    ! 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 将 javascript 重定向到 http://quotes.money.163.com/stock,更糟糕的是,curl 既不理解 javascript 重定向也不理解 html 重定向。如果您想要理解这两者的东西,请考虑使用无头浏览器。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    相关资源
    最近更新 更多