如何不打开网页的情况下得到跳转网址的最后网址续
原贴http://www.autoitx.com/thread-44892-1-1.html
其中2楼和9楼给了答案,但是后来试了几个网址,如https类的,和location返回的不是完全长度的网址,比如以/开头,那么上述贱两种方法都失效了,请问,代码怎么优化才能适用所有的网址呢?
下面是失效后的代码:
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.12.0
Author: myName
Script Function:
Template AutoIt script.
#ce ----------------------------------------------------------------------------
; Script Start - Add your code below here
#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://www.sciencemag.org/cgi/pmidlookup?view=long&pmid=9575084"
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
http://www.jimmunol.org/cgi/pmidlookup?view=long&pmid=5838756
这个地址也是跳转很多次,中间出现location不全的现象,我自已试着写一个通用的吧。 本帖最后由 sex123 于 2015-3-20 17:59 编辑
我自已试着写了写,感觉可以应对大部分的跳转网址了,以后如果再有问题,再改一改就差不多了,不知道这个代码怎么做成udf。
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.12.0
Author: myName
Script Function:
Template AutoIt script.
#ce ----------------------------------------------------------------------------
; Script Start - Add your code below here
#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)"
;Local $sU = "http://www.sciencemag.org/cgi/pmidlookup?view=long&pmid=9575084"
local $sU = "http://www.jimmunol.org/cgi/pmidlookup?view=long&pmid=5838756"
;local $sU = "http://joi.jlc.jst.go.jp/DN/JST.JSTAGE/circj/CJ-13-1103?from=PubMed"
;local $sU = "http://dx.doi.org/10.5692/clinicalneurol.54.1171"
;Local $sU = "http://www.baidu.com"
;Local $sU ="http://dx.doi.org/10.1038/ni.2417"
Local $sUarray
Local $sUtimes=0
Local $sUori =$sU
While $sU <>""
Dim $aU, $i = 0
While $sU <> ""
ReDim $aU[$i+1]
$sU = _get_location($sU)
$aU[$i] = $sU
$i += 1
WEnd
If $i> 1 Then
;_ArrayDisplay($aU)
;MsgBox(64, "i", $i)
;MsgBox(64, "aui-2", $aU[$i-2])
$lasturl=$aU[$i-2]
$sU=$lasturl
if StringInStr($lasturl, "://")=0 and $i >2 Then
$domainurl=$aU[$i-3]
$domain=_get_domain($domainurl)
; MsgBox(0,"domain",$domain)
if StringLeft($lasturl,1)<>"/" then $lasturl="/"&$lasturl
$addurl=$domain&$lasturl
$sU=$addurl
;MsgBox(0,"sU",$sU)
EndIf
EndIf
_ArrayAdd($sUarray, $sU)
$sUtimes+=1
WEnd
;MsgBox(0,"final",$sU)
;_ArrayDisplay($sUarray)
;MsgBox(0,"sutime",$sUtimes)
$allurlubound=UBound($sUarray)
if $allurlubound >1 Then
$finalurl=$sUarray[$allurlubound-2]
Else
$finalurl=$sUori
EndIf
MsgBox(0,"Finalurl",$finalurl)
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
Func _get_domain($hwUrl)
$sUrl = StringSplit($hwUrl, "/")
;_ArrayDisplay($surl)
$surlbound = UBound($sUrl)
;MsgBox(0, 0, $surlbound)
If $surlbound > 4 Then
;_ArrayDisplay($surlbound)
$surltop = $sUrl & "//" & $sUrl
Else
$surltop = $sU
EndIf
Return $surltop
EndFunc ;==>_get_domain
thanks for sharing,marked 邦你顶{:face (427):} 我又把这个功能改成自定义func了,而且去掉了ARRAY.AU3,加了sleep,防封ip,应该还可以精简代码,但是自已不会了。先记录一下吧。
#Region
#AccAu3Wrapper_Icon= ;程序图标
#AccAu3Wrapper_UseX64=n ;是否编译为64位程序(y/n)
#AccAu3Wrapper_OutFile= ;输出的Exe名称
#AccAu3Wrapper_OutFile_x64= ;64位输出的Exe名称
#AccAu3Wrapper_UseUpx=n ;是否使用UPX压缩(y/n) 注:开启压缩极易引起误报问题
#AccAu3Wrapper_Res_Comment= ;程序注释
#AccAu3Wrapper_Res_Description= ;程序描述
#AccAu3Wrapper_Res_Fileversion=1.0.0.1 ;文件版本
#AccAu3Wrapper_Res_FileVersion_AutoIncrement=n ;自动更新版本 y/n/p=自动/不自动/询问
#AccAu3Wrapper_Res_ProductVersion=1.0 ;产品版本
#AccAu3Wrapper_Res_Language=2052 ;资源语言, 英语=2057/中文=2052
#AccAu3Wrapper_Res_LegalCopyright= ;程序版权
#AccAu3Wrapper_Res_RequestedExecutionLevel= ;请求权限: None/asInvoker/highestAvailable/requireAdministrator
#AccAu3Wrapper_Res_Field=Au3.Cc|Au3编程学习网 ;自定义资源段, 格式: [名称]|[值]
#AccAu3Wrapper_Run_Tidy=y ;编译前自动整理脚本(y/n)
#AccAu3Wrapper_Run_Obfuscator=y ;启用脚本加密(y/n)
#Obfuscator_Parameters=/cs=1 /cn=1 /cf=1 /cv=1 /sf=1 /sv=1 ;脚本加密参数: 0/1不加密/加密, /cs字符串 /cn数字 /cf函数名 /cv变量名 /sf精简函数 /sv精简变量
#AccAu3Wrapper_DBSupport=y ;使字符串加密支持双字节字符(y/n) <- 可对中文字符等实现字符串加密
#AccAu3Wrapper_AntiDecompile=y ;是否启用防反功能(y/n) <- 简单防反, 用于应对傻瓜式反编译工具
#EndRegion
#cs ____________________________________
欢迎使用 AutoIt v3 中文版 !
Au3编程资源网:http://au3.net/
Au3编程学习网:http://au3.cc/
Au3编程博客: http://au3.me/
Au3版本: 3.3.12.0
脚本作者:
脚本功能:
更新日志:
联系方式:
#ce _______________脚本开始_________________
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.12.0
Author: myName
Script Function:
Template AutoIt script.
#ce ----------------------------------------------------------------------------
; Script Start - Add your code below here
#PRE_UseX64=n
;#include <Array.au3>
;Local $sU = "http://www.sciencemag.org/cgi/pmidlookup?view=long&pmid=9575084"
Local $sU = "http://www.jimmunol.org/cgi/pmidlookup?view=long&pmid=5838756"
;Local $sU = "http://joi.jlc.jst.go.jp/DN/JST.JSTAGE/circj/CJ-13-1103?from=PubMed"
;local $sU = "http://dx.doi.org/10.5692/clinicalneurol.54.1171"
;Local $sU = "http://www.baidu.com"
;Local $sU = "http://dx.doi.org/10.1038/ni.2417"
$finalurl = _posturl($sU)
MsgBox(0, "Finalurl", $finalurl)
Func _posturl($sU)
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 $sUarray
Local $sUAll = ""
Local $sUtimes = 0
Local $sUori = $sU
While $sU <> ""
Dim $aU, $i = 0
While $sU <> ""
ReDim $aU[$i + 1]
$sU = _get_location($sU)
$aU[$i] = $sU
$i += 1
Sleep(500);防网址封ip,延迟时间自定
WEnd
If $i > 1 Then
;_ArrayDisplay($aU)
;MsgBox(64, "i", $i)
;MsgBox(64, "aui-2", $aU[$i-2])
$lasturl = $aU[$i - 2]
$sU = $lasturl
If StringInStr($lasturl, "://") = 0 And $i > 2 Then
$domainurl = $aU[$i - 3]
$domain = _get_domain($domainurl)
; MsgBox(0,"domain",$domain)
If StringLeft($lasturl, 1) <> "/" Then $lasturl = "/" & $lasturl
$addurl = $domain & $lasturl
$sU = $addurl
;MsgBox(0,"sU",$sU)
EndIf
EndIf
;_ArrayAdd($sUarray, $sU)
$sUAll = $sUAll & @CRLF & $sU
$sUtimes += 1
WEnd
;MsgBox(0,"final",$sU)
;_ArrayDisplay($sUarray)
;MsgBox(0, "sutime", $sUtimes)
;MsgBox(0, "sUAll", $sUAll)
$finalurlarray = StringSplit($sUAll, @CRLF, 1)
$uboundfinalurl = UBound($finalurlarray)
;MsgBox(0, "uboundfinalurl", $uboundfinalurl)
;_ArrayDisplay($finalurlarray)
If IsArray($finalurlarray) = 1 Then
If $uboundfinalurl > 3 Then
$finalurl = $finalurlarray[$uboundfinalurl - 2]
Else
$finalurl = $sUori
EndIf
Else
MsgBox(0, "wrong", "posturlwrong")
EndIf
;$allurlubound = UBound($sUarray)
;If $allurlubound > 1 Then
;$finalurl = $sUarray[$allurlubound - 2]
;Else
; $finalurl = $sUori
;EndIf
Return $finalurl
EndFunc ;==>_posturl
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 ;==>_get_location
Func _get_domain($hwUrl)
$sUrl = StringSplit($hwUrl, "/")
;_ArrayDisplay($surl)
$surlbound = UBound($sUrl)
;MsgBox(0, 0, $surlbound)
If $surlbound > 4 Then
;_ArrayDisplay($surlbound)
$surltop = $sUrl & "//" & $sUrl
Else
$surltop = $sU
EndIf
Return $surltop
EndFunc ;==>_get_domain
页:
[1]