代码之家  ›  专栏  ›  技术社区  ›  vise

重定向curl后获取url

  •  79
  • vise  · 技术社区  · 14 年前

    我需要得到一个页面重定向后最好与卷曲或wget最终的网址。

    例如 http://google.com 可以重定向到 http://www.google.com .

    内容很容易获取(例如。 curl --max-redirs 10 http://google.com -L ),但我只对最终的url感兴趣(在前一种情况下) ).

    8 回复  |  直到 5 年前
        1
  •  223
  •   Daniel Stenberg    4 年前

    curl -w option url_effective 你就是这样 寻找。

    curl -Ls -o /dev/null -w %{url_effective} http://google.com
    

    更多信息

    -L         Follow redirects
    -s         Silent mode. Don't output anything
    -o FILE    Write output to <file> instead of stdout
    -w FORMAT  What to output after completion
    

    更多

    您可能需要添加 -I i )同样,这将使命令不下载任何“body”,但它随后也使用HEAD方法,这不是问题所包含的内容,并有可能改变服务器的功能。有时服务器对HEAD的响应不好,即使它们对GET的响应很好。

        2
  •  31
  •   Spooky Muscothym    4 年前

    谢谢,这对我很有帮助。我做了一些改进,并将其包装在助手脚本“finalur”中:

    #!/bin/bash
    curl $1 -s -L -I -o /dev/null -w '%{url_effective}'
    
    • -o 输出到 /dev/null
    • -I
    • -s 静默模式,无进度条

    这样就可以从以下其他脚本调用命令:

    echo `finalurl http://someurl/`
    
        3
  •  8
  •   Gavin Mogan    14 年前

    作为另一种选择:

    $ curl -i http://google.com
    HTTP/1.1 301 Moved Permanently
    Location: http://www.google.com/
    Content-Type: text/html; charset=UTF-8
    Date: Sat, 19 Jun 2010 04:15:10 GMT
    Expires: Mon, 19 Jul 2010 04:15:10 GMT
    Cache-Control: public, max-age=2592000
    Server: gws
    Content-Length: 219
    X-XSS-Protection: 1; mode=block
    
    <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
    <TITLE>301 Moved</TITLE></HEAD><BODY>
    <H1>301 Moved</H1>
    The document has moved
    <A HREF="http://www.google.com/">here</A>.
    </BODY></HTML>
    

    但它不会超过第一个。

        4
  •  6
  •   CrandellWS    11 年前

    你可以用wget来做这个。 wget --content-disposition 如果您添加 -O /dev/null 您将不会实际保存该文件。

    wget -O /dev/null --content-disposition example.com

        5
  •  5
  •   vise    14 年前

    非常感谢。我最终实现了你的建议:curl-I+grep

    curl -i http://google.com -L | egrep -A 10 '301 Moved Permanently|302 Found' | grep 'Location' | awk -F': ' '{print $2}' | tail -1
    

    可能是四轮马车,但乍一看还行。

        6
  •  3
  •   Mike Q    7 年前

    这将起作用:

     curl -I somesite.com | perl -n -e '/^Location: (.*)$/ && print "$1\n"'
    
        7
  •  3
  •   Dave Baird    4 年前

    curl

    #!/bin/bash
    real_url () {
        printf 'location.href\nquit\n' | \
        chromium-browser --headless --disable-gpu --disable-software-rasterizer \
        --disable-dev-shm-usage --no-sandbox --repl "$@" 2> /dev/null \
        | tr -d '>>> ' | jq -r '.result.value'
    }
    

    如果没有安装chrome,可以从docker容器中使用它:

    #!/bin/bash
    real_url () {
        printf 'location.href\nquit\n' | \
        docker run -i --rm --user "$(id -u "$USER")" --volume "$(pwd)":/usr/src/app \
        zenika/alpine-chrome --no-sandbox --repl "$@" 2> /dev/null \
        | tr -d '>>> ' | jq -r '.result.value'
    }
    

    像这样:

    $ real_url http://dx.doi.org/10.1016/j.pgeola.2020.06.005 
    https://www.sciencedirect.com/science/article/abs/pii/S0016787820300638?via%3Dihub
    
        8
  •  2
  •   Community paulsm4    4 年前

    参数 -L (--location) -I (--head) 仍然对位置url执行不必要的HEAD请求。

    此代码仅对指定的URL执行一个HEAD请求,并从位置标头获取重定向URL:

    curl --head --silent --write-out "%{redirect_url}\n" --output /dev/null "https://""goo.gl/QeJeQ4"
    

    速度试验

    all_videos_link.txt -50个指向youtube的goo.gl+bit.ly链接

    1.跟随位置

    time while read -r line; do
        curl -kIsL -w "%{url_effective}\n" -o /dev/null  $line
    done < all_videos_link.txt
    

    结果:

    real    1m40.832s
    user    0m9.266s
    sys     0m15.375s
    

    2.无跟随位置

    time while read -r line; do
        curl -kIs -w "%{redirect_url}\n" -o /dev/null  $line
    done < all_videos_link.txt
    

    real    0m51.037s
    user    0m5.297s
    sys     0m8.094s
    
        9
  •  1
  •   Gavin Mogan    14 年前

    我不知道如何使用curl,但是libwww perl安装GET别名。

    $ GET -S -d -e http://google.com
    GET http://google.com --> 301 Moved Permanently
    GET http://www.google.com/ --> 302 Found
    GET http://www.google.ca/ --> 200 OK
    Cache-Control: private, max-age=0
    Connection: close
    Date: Sat, 19 Jun 2010 04:11:01 GMT
    Server: gws
    Content-Type: text/html; charset=ISO-8859-1
    Expires: -1
    Client-Date: Sat, 19 Jun 2010 04:11:01 GMT
    Client-Peer: 74.125.155.105:80
    Client-Response-Num: 1
    Set-Cookie: PREF=ID=a1925ca9f8af11b9:TM=1276920661:LM=1276920661:S=ULFrHqOiFDDzDVFB; expires=Mon, 18-Jun-2012 04:11:01 GMT; path=/; domain=.google.ca
    Title: Google
    X-XSS-Protection: 1; mode=block
    
        10
  •  0
  •   Lakshmikandan    5 年前

    #!/bin/bash 
    LOCATION=`curl -I 'http://your-domain.com/url/redirect?r=something&a=values-VALUES_FILES&e=zip' | perl -n -e '/^Location: (.*)$/ && print "$1\n"'` 
    echo "$LOCATION"
    

    注意:当您执行curl-I命令时 http://your-domain.com curl -I 'http://your-domain.com'

        11
  •  -4
  •   SpliFF    14 年前

    你可以用grep。wget没有告诉你它在哪里重定向吗?把它说出来。