本帖最后由 holley 于 2023-10-10 11:04 编辑
原贴来自:怎么正则提取网页内容【已解决】-已解决问题区-AUTOIT CN - Powered by Autoit中文论坛 (autoitx.com)
火绒的真实请求地址:http://www.huorong.cn/versionShow.php
国庆前,正常,今天第一天上班更新发现无法下载了
原脚本:#include <Inet.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ProgressConstants.au3>
Opt("GUIOnEventMode", 1)
Local $sSource = _INetGetSource('http://www.huorong.cn/versionShow.php')
Local $sSRERe = StringRegExpReplace($sSource, '^.+urlAll":"(h.+?:)[\\/]+([\w.]+)[\\/]+([^"]+).+$', '\1/\/\2/\3')
;~ Local $sSRERe = StringRegExpReplace($sSource, '(?i)^.+All":"(h.+?:)[\\/]+([\w.]+)[\\/]+([^"]+).+$', '\1/\/\2/\3')
Local $xName = StringRegExpReplace($sSource, '^.+urlAll":"(h.+?:)[\\/]+([\w.]+)[\\/]+([^"]+).+$', '\3')
$form = GUICreate(StringFormat($xName) & "下载", 600, 120, -1, -1)
Local $xUrl = StringFormat($sSRERe)
Local $nSize = InetGetSize($xUrl)
Local $progressbar1 = GUICtrlCreateProgress(10, 10, 550, 16)
Local $sLabel = GUICtrlCreateLabel("", 570, 12, 30, 16)
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
GUISetState()
Local $hDownload = InetGet($xUrl, @ScriptDir & "\" & $xName, 1, 1) ;不读缓存,后台下载
Do
Local $aData = InetGetInfo($hDownload) ;
GUICtrlSetData($progressbar1, Int($aData[0] / $nSize * 100))
GUICtrlSetData($sLabel, Int($aData[0] / $nSize * 100) & "%")
Sleep(200)
Until ($aData[2])
InetClose($hDownload)
WinClose(StringFormat($xName) & "下载")
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
Func Quit()
Exit
EndFunc ;==>Quit
测试发现 _INetGetSource无法有效获取到网页内容,后改为Http.au3库文件获取,已正常
但同时又发现:InetGet已无法有效下载到$xUrl文件,网页直接下载或下载工具使用正常。(已添加$sUserAgent全局UA)
这属于服务器限制了 还是我电脑端的问题???我系统win10 专业版。
补充故障现象:
[attachg]38143[/attachg]
附件已附上Http.au3库文件。Local $sSource =_HTTP_Get("https://www.huorong.cn/versionShow.php")
翻英文站继续折腾,发现WinHttp库可以下载,但下载下来的东西不对,,,奇怪奇怪真奇怪!#include <FileConstants.au3>
#include <StringConstants.au3>
#include "WinHttp.au3"
_Example()
Func _Example()
Local $sDomain = "www.huorong.cn"
Local $sPage = "/versionShow.php"
$hOpen = _WinHttpOpen("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0")
$hConnect = _WinHttpConnect($hOpen, $sDomain)
$hRequest = _WinHttpOpenRequest($hConnect, "POST", $sPage)
_WinHttpSendRequest($hRequest, "Content-Type: application/x-www-form-urlencoded")
_WinHttpReceiveResponse($hRequest)
Dim $sReturned
If _WinHttpQueryDataAvailable($hRequest) Then ; 如果有数据
Do
$sReturned &= _WinHttpReadData($hRequest)
Until @error
EndIf ; 关闭句柄
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
Local $sSRERe = StringRegExpReplace($sReturned, '^.+urlAll":"(h.+?:)[\\/]+([\w.]+)[\\/]+([^"]+).+$', '\1/\/\2/\3')
Local $sSRERe1 = StringRegExpReplace($sReturned, '^.+urlAll":"(h.+?:)[\\/]+([\w.]+)[\\/]+([^"]+).+$', '\1')
Local $sSRERe2 = StringRegExpReplace($sReturned, '^.+urlAll":"(h.+?:)[\\/]+([\w.]+)[\\/]+([^"]+).+$', '\2')
Local $xName = StringRegExpReplace($sReturned, '^.+urlAll":"(h.+?:)[\\/]+([\w.]+)[\\/]+([^"]+).+$', '\3')
; "YOUR_SOURCE_URL"
Local $EXAMPLE_URL = StringFormat($sSRERe)
; "YOUR_DESTINATION"
Local $LOCAL_FILE = @ScriptDir & "" & $xName
;~ _WinHTTP_GetAndSaveFileToDisc_Ex($EXAMPLE_URL, $LOCAL_FILE, 0)
_WinHTTP_GetAndSaveFileToDisc_Ex($EXAMPLE_URL, $LOCAL_FILE, 1)
;~ _WinHTTP_GetAndSaveFileToDisc_Ex($EXAMPLE_URL, $LOCAL_FILE, 2)
EndFunc ;==>_Example
Func _WinHTTP_GetAndSaveFileToDisc_Ex($EXAMPLE_URL, $LOCAL_FILE, $iMode = 1)
Local $hTimer = TimerInit()
Local $hOpen = _WinHttpOpen() ; Initialize and get session handle
Local $s_URL_Domain = StringRegExpReplace($EXAMPLE_URL, '(?i)(.*?\..*?)(\/.+)', '$1')
Local $hConnect = _WinHttpConnect($hOpen, $s_URL_Domain, $INTERNET_DEFAULT_HTTPS_PORT) ; Get connection handle
Local $s_URL_File = StringReplace($EXAMPLE_URL, $s_URL_Domain, '')
Local $hRequest = _WinHttpOpenRequest($hConnect, Default, $s_URL_File, Default, Default, Default, $WINHTTP_FLAG_SECURE)
If @error Then ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & _
" : _WinHttpOpenRequest" & @CRLF)
_WinHttpSendRequest($hRequest) ; Send request
_WinHttpReceiveResponse($hRequest) ; Wait for the response
ProgressOn("Downloading", "In __WinHTTP_DownloadProgress...")
__WinHTTP_DownloadProgress(_WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_CONTENT_LENGTH))
Local $sData, $sChunk
If _WinHttpQueryDataAvailable($hRequest) Then ; Check if there is data available...
While 1
$sChunk = _WinHttpReadData_Ex($hRequest, $iMode, Default, Default, __WinHTTP_DownloadProgress)
If @error Then ExitLoop
$sData &= $sChunk
;~ Sleep(50)
WEnd
Else
MsgBox(48, "Error", "Site is experiencing problems (or you).")
EndIf
Sleep(100)
ProgressOff()
; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
If Not @Compiled Then ConsoleWrite("! IFNC: BinaryLen($sData) = " & BinaryLen($sData) & @CRLF)
Local $hFile = FileOpen($LOCAL_FILE, $FO_OVERWRITE + $FO_CREATEPATH + $FO_BINARY + $FO_UTF8)
FileWrite($hFile, $sData)
FileClose($hFile)
If Not @Compiled Then ConsoleWrite("> IFNC: " & TimerDiff($hTimer) / 1000 & @CRLF)
EndFunc ;==>_WinHTTP_GetAndSaveFileToDisc_Ex
Func __WinHTTP_DownloadProgress($iSizeAll, $iSizeChunk = 0)
Local Static $iMax, $iCurrentSize
If $iSizeAll Then $iMax = $iSizeAll
$iCurrentSize += $iSizeChunk
If Not @Compiled Then ConsoleWrite("! IFNC: $iCurrentSize = " & $iCurrentSize & @CRLF)
Local Static $iPercent_static = 0
Local $iPercent = Round($iCurrentSize / $iMax * 100, 0)
If $iPercent_static <> $iPercent Then
$iPercent_static = $iPercent
ProgressSet($iPercent, $iPercent & " %")
EndIf
EndFunc ;==>__WinHTTP_DownloadProgress
Func _WinHttpReadData_Ex($hRequest, $iMode = Default, $iNumberOfBytesToRead = Default, $pBuffer = Default, $vFunc = Default)
__WinHttpDefault($iMode, 0)
;~ __WinHttpDefault($iNumberOfBytesToRead, 8192)
__WinHttpDefault($iNumberOfBytesToRead, 1024)
__WinHttpDefault($vFunc, 0)
Local $tBuffer, $vOutOnError = ""
If $iMode = 2 Then $vOutOnError = Binary($vOutOnError)
Switch $iMode
Case 1, 2
If $pBuffer And $pBuffer <> Default Then
$tBuffer = DllStructCreate("byte[" & $iNumberOfBytesToRead & "]", $pBuffer)
Else
$tBuffer = DllStructCreate("byte[" & $iNumberOfBytesToRead & "]")
EndIf
Case Else
$iMode = 0
If $pBuffer And $pBuffer <> Default Then
$tBuffer = DllStructCreate("char[" & $iNumberOfBytesToRead & "]", $pBuffer)
Else
$tBuffer = DllStructCreate("char[" & $iNumberOfBytesToRead & "]")
EndIf
EndSwitch
Local $sReadType = "dword*"
If BitAND(_WinHttpQueryOption(_WinHttpQueryOption(_WinHttpQueryOption($hRequest, $WINHTTP_OPTION_PARENT_HANDLE), $WINHTTP_OPTION_PARENT_HANDLE), $WINHTTP_OPTION_CONTEXT_VALUE), $WINHTTP_FLAG_ASYNC) Then $sReadType = "ptr"
Local $aCall = DllCall($hWINHTTPDLL__WINHTTP, "bool", "WinHttpReadData", _
"handle", $hRequest, _
"struct*", $tBuffer, _
"dword", $iNumberOfBytesToRead, _
$sReadType, 0)
If @error Or Not $aCall[0] Then Return SetError(1, 0, "")
If Not $aCall[4] Then Return SetError(-1, 0, $vOutOnError)
If IsFunc($vFunc) Then $vFunc(0, $aCall[4])
Local $dBinary = DllStructGetData($tBuffer, 1)
If $aCall[4] < $iNumberOfBytesToRead Then
Switch $iMode
Case 0
Return SetExtended($aCall[4], StringLeft($dBinary, $aCall[4]))
Case 1
Return SetExtended($aCall[4], BinaryToString(BinaryMid($dBinary, 1, $aCall[4]), $SB_ANSI))
Case 2
Return SetExtended($aCall[4], BinaryMid($dBinary, 1, $aCall[4]))
EndSwitch
Else
Switch $iMode
Case 0, 2
Return SetExtended($aCall[4], $dBinary)
Case 1
Return SetExtended($aCall[4], BinaryToString($dBinary, $SB_ANSI))
EndSwitch
EndIf
EndFunc ;==>_WinHttpReadData_Ex
控制台可以看到,当前大小读取是正常的,但最后保存的时候 感觉像截断了保存下来的。
|