sex123 发表于 2014-7-26 22:32:49

如何不打开网页的情况下得到跳转网址的最后网址

http://dx.doi.org/10.1038/ni.2417
点击后,网页跳转几次,最终的网址是
http://www.nature.com/ni/journal/v13/n11/full/ni.2417.html
如何不打开网页,不调用ie的情况下,得到最终的网址呢?

zch11230 发表于 2014-7-27 10:04:57

本帖最后由 zch11230 于 2014-7-27 13:07 编辑

虽然tcpsocket效率低了点 但试了很久就找到这个方法有效 其它的方法试了抓包看header都有返回重定向后的地址 但是就是取不到header里面Location的值。
或者调用工具 curl 可以很方便快速的获取到。
udf改自http://www.autoitx.com/forum.php?mod=viewthread&tid=19390$nurl=StringRegExp(WebRequest("http://dx.doi.org/10.1038/ni.2417"),"Location: (\V+)",3)
If IsArray($nurl) Then MsgBox(0,"",$nurl)
Func WebRequest($str, $method="GET", $data="")
       Local $host,$url
           $host=StringRegExpReplace($str,"(?:http://)?((?:\w+\.)*\w+\.\w+).*","$1")
           $url=StringRegExpReplace($str,"(?:http://)?(?:\w+\.)*\w+\.\w+(.*)","$1")
;~                 MsgBox(0,$host,$url)
      TCPStartup()
      Local $socket = -1
      $socket = TCPConnect(TCPNameToIP($host), "80")
      Local $sData = $method & " "& $url &" HTTP/1.1" & @CRLF
      $sData &= "Accept: */*" & @CRLF
      $sData &= "Accept-Language: zh-CN" & @CRLF
      $sData &= "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2;)" & @CRLF
      If $method = "POST" Then $sData &= "Content-Type: application/x-www-form-urlencoded" & @CRLF
      $sData &= "Host: " & $host & @CRLF
      $sData &= "Content-Length: " & StringLen($data) & @CRLF
      $sData &= "Connection: Keep-Alive" & @CRLF
      $sData &= "Pragma: no-cache" & @CRLF
      $sData &= @CRLF
      $sData &= $data
      TCPSend($socket, $sData)
       Local $text = "", $i = 0
        Do
                Local $r = TCPRecv($socket, 1024)
                If @error <> 0 Then ExitLoop
                If $r <> "" Then
                        $text &= $r
                        $i=0
                Else
                        $i += 1
                EndIf
                ConsoleWrite($r)
        Until $i > 1024
      TCPCloseSocket($socket)
      TCPShutdown()
      Return $text
EndFunc

sex123 发表于 2014-7-27 11:19:51

虽然tcpsocket效率低了点 但试了很久就找到这个方法有效 其它的方法试了抓包看header都有返回重定向后的地址 ...
zch11230 发表于 2014-7-27 10:04 http://www.autoitx.com/images/common/back.gif


    能说说怎么调用curl我去英文的论坛看了看curl.au3这个udf,没发现怎么读location,只有读网页内容的命令。头疼。 。

zch11230 发表于 2014-7-27 12:54:38

回复 3# sex123


   我是用的命令行下的curl 直接curl.exe www.xxx.com 就可以得到返回的数据 不过上面的代码 原作者所提的效率低的主要原因是后面反复在接收无用数据 我加了个判断 如果连续收到多少空数据 就不接收了 感觉效率还可以吧,应该够用了。你如果再加句If StringInStr($r,"Location") ThenExitLoop 还要快点。

sex123 发表于 2014-7-27 15:23:37

回复sex123


   我是用的命令行下的curl 直接curl.exe就可以得到返回的数据 不过上面的代码 原作者 ...
zch11230 发表于 2014-7-27 12:54 http://www.autoitx.com/images/common/back.gif


    是在autoitx模式下用命令行来curl吗?这种代码怎么写呢?

zch11230 发表于 2014-7-27 15:40:51

回复 5# sex123


    DOS下的curl 置顶有说明如何获取回显2楼的代码已经改过 够用了 不用纠结curl。
http://www.autoitx.com/thread-175-1-1.html

komaau3 发表于 2014-7-27 16:39:15

回复 1# sex123

#PRE_UseX64=n                                                                       

Global $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
$oHTTP.Option(0) = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)"

Local $U1 = get_location("http://dx.doi.org/10.1038/ni.2417")
ConsoleWrite($U1 & @CRLF)

Local $U2 = get_location($U1)
ConsoleWrite($U2 & @CRLF)


Func get_location($sUrl, $sRef = "")
        Local $sBody, $aMatch, $sReferer
        $oHTTP.Option(6) = False
        $oHTTP.Open("GET", $sUrl, True)
        If $sRef <> "" Then $oHTTP.setRequestHeader("Referer", $sRef)
        $oHTTP.Send()
        $oHTTP.WaitForResponse(-1)

        $sUrl = $oHTTP.getResponseHeader("Location")
        ;ConsoleWrite($oHTTP.getAllResponseHeaders() & @CRLF)

        Return $sUrl
EndFunc

sex123 发表于 2014-7-27 16:41:06

回复sex123
komaau3 发表于 2014-7-27 16:39 http://www.autoitx.com/images/common/back.gif


    这个方法我试过,这个url只是转了两次,而有的url可以转三次,四次,或者更多,那样的话,怎么办呢?

komaau3 发表于 2014-7-28 07:37:42

回复 8# sex123 #PRE_UseX64=n
#include <Array.au3>

Global $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
$oHTTP.Option(0) = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)"

Dim $aU, $i = 0

Local $sU = "http://dx.doi.org/10.1038/ni.2417"

While $sU <> ""
        ReDim $aU[$i+1]
        $sU = get_location($sU)
        $aU[$i] = $sU
        $i += 1
WEnd

_ArrayDisplay($aU)
MsgBox(64, "", $aU[$i-2])

Func get_location($sUrl, $sRef = "")
        Local $sBody, $aMatch, $sReferer
        $oHTTP.Option(6) = False
        $oHTTP.Open("HEAD", $sUrl, True)
        If $sRef <> "" Then $oHTTP.setRequestHeader("Referer", $sRef)
        $oHTTP.Send()
        $oHTTP.WaitForResponse(-1)

        $sUrl = $oHTTP.getResponseHeader("Location")
        Return $sUrl
EndFunc

likeping 发表于 2014-8-2 11:41:44

路过 学习一下

49666684 发表于 2014-8-2 16:42:24

路过 学习一下

haijie1223 发表于 2014-8-21 06:37:56

回复 9# komaau3


递归应该可以

wangms 发表于 2014-9-10 21:26:09

学习啦。。。。。。。。。。。。。。。。。

tvzml 发表于 2014-9-12 13:28:16

看看 。。。。非常谢谢

sdc7 发表于 2014-9-12 14:37:13

这不是在构造POST吗?~
页: [1]
查看完整版本: 如何不打开网页的情况下得到跳转网址的最后网址