找回密码
 加入
搜索
查看: 14569|回复: 14

[系统综合] 如何不打开网页的情况下得到跳转网址的最后网址

 火.. [复制链接]
发表于 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的情况下,得到最终的网址呢?
发表于 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[0])
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
 楼主| 发表于 2014-7-27 11:19:51 | 显示全部楼层
虽然tcpsocket效率低了点 但试了很久就找到这个方法有效 其它的方法试了抓包看header都有返回重定向后的地址 ...
zch11230 发表于 2014-7-27 10:04



    能说说怎么调用curl我去英文的论坛看了看curl.au3这个udf,没发现怎么读location,只有读网页内容的命令。头疼。 。
发表于 2014-7-27 12:54:38 | 显示全部楼层
回复 3# sex123


   我是用的命令行下的curl 直接curl.exe www.xxx.com 就可以得到返回的数据 不过上面的代码 原作者所提的效率低的主要原因是后面反复在接收无用数据 我加了个判断 如果连续收到多少空数据 就不接收了 感觉效率还可以吧,应该够用了。你如果再加句If StringInStr($r,"Location") Then  ExitLoop 还要快点。
 楼主| 发表于 2014-7-27 15:23:37 | 显示全部楼层
回复  sex123


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



    是在autoitx模式下用命令行来curl吗?这种代码怎么写呢?
发表于 2014-7-27 15:40:51 | 显示全部楼层
回复 5# sex123


    DOS下的curl 置顶有说明如何获取回显  2楼的代码已经改过 够用了 不用纠结curl。
http://www.autoitx.com/thread-175-1-1.html
发表于 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
 楼主| 发表于 2014-7-27 16:41:06 | 显示全部楼层
回复  sex123
komaau3 发表于 2014-7-27 16:39



    这个方法我试过,这个url只是转了两次,而有的url可以转三次,四次,或者更多,那样的话,怎么办呢?
发表于 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[1], $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
发表于 2014-8-2 11:41:44 | 显示全部楼层
路过 学习一下
发表于 2014-8-2 16:42:24 | 显示全部楼层
路过 学习一下
发表于 2014-8-21 06:37:56 | 显示全部楼层
回复 9# komaau3


  递归应该可以
发表于 2014-9-10 21:26:09 | 显示全部楼层
学习啦。。。。。。。。。。。。。。。。。
发表于 2014-9-12 13:28:16 | 显示全部楼层
看看 。。。。非常谢谢
发表于 2014-9-12 14:37:13 | 显示全部楼层
这不是在构造POST吗?~
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-26 05:37 , Processed in 0.082202 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表