【已解决】用au3的InetGet下载文件错误
本帖最后由 3131210 于 2024-6-29 21:21 编辑代码如下,前面两段获取json数据都是对的,但是到最后下载文件的时候就提示错误了。复制连接出来用浏览器也是可以下载的,不知道是什么问题
下载到的文件内容如图
#include <Inet.au3>
$sFileUrl = 'https://huankong233.lanzoue.com/b0eay044h#35su'
_FileDown($sFileUrl)
Func _FileDown($Url)
Local $tempJson, $temparr, $tempFileName, $tempFilePath = @ScriptDir & '\'
Local $sFileUrlSplit = StringSplit($Url, '#', 1)
$tempJson = BinaryToString(InetRead('https://api.huankong.top/lanzou/?url=' & $sFileUrlSplit & '&pass=' & $sFileUrlSplit, 1 + 2))
ConsoleWrite($tempJson & @CRLF)
$temparr = StringRegExp(BinaryToString($tempJson), '(?i)fileName.+?\:"(.+?)".+?fileId"\:"(.+?)"\}', 3)
If @error Then Return
$tempFileName = $temparr
FileDelete($tempFilePath & $tempFileName)
If FileExists($tempFilePath & $tempFileName) Then Return ;无法删除文件
$tempJson = BinaryToString(InetRead('https://api.huankong.top/lanzou/?fileId=' & $temparr, 1 + 2))
ConsoleWrite($tempJson & @CRLF)
$temparr = StringRegExp(BinaryToString($tempJson), '(?i)fileUrl"\:"(.+?)"\}', 3)
If @error Then Return
ConsoleWrite(StringReplace($temparr, '\', '') & @CRLF)
Local $hDownload = InetGet(StringReplace($temparr, '\', ''), $tempFilePath & $tempFileName, 1 + 2, 1)
Do
Sleep(100)
Until InetGetInfo($hDownload, 2)
InetClose($hDownload)
ConsoleWrite(_FileRead($tempFilePath & $tempFileName) & @CRLF)
EndFunc ;==>_FileDown
Func _FileRead($Path)
Local $hFileOpen = FileOpen($Path, 0)
If $hFileOpen = -1 Then Return
Local $sFileRead = FileRead($hFileOpen)
FileClose($hFileOpen)
Return $sFileRead
EndFunc ;==>_FileRead
本帖最后由 3131210 于 2024-6-29 21:26 编辑
我也改好了一个一样的
$sFileUrl = 'https://huankong233.lanzoue.com/b0eay044h#35su'
_FileDown($sFileUrl)
Func _FileDown($Url)
Local $tempJson, $temparr, $tempFileName, $tempFilePath = @ScriptDir & '\'
Local $sFileUrlSplit = StringSplit($Url, '#', 1)
Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
If @error Then Return
$oHTTP.Option(4) = 13056 ;忽略错误标志
$oHTTP.Option(6) = True ;是否接收重定向地址信息
$oHTTP.SetTimeouts(60000, 60000, 60000, 60000)
$tempJson = BinaryToString(_GetData($oHTTP, 'https://api.huankong.top/lanzou/?url=' & $sFileUrlSplit & '&pass=' & $sFileUrlSplit), 1)
;ConsoleWrite($tempJson & @CRLF)
$temparr = StringRegExp($tempJson, '(?i)^.+?fileName.+?\:"(.+?)".+?fileId"\:"(.+?)"\}', 3)
If @error Then
$oHTTP = 0
Return
EndIf
$tempFileName = $temparr
$tempJson = BinaryToString(_GetData($oHTTP, 'https://api.huankong.top/lanzou/?fileId=' & $temparr), 1)
;ConsoleWrite($tempJson & @CRLF)
$temparr = StringRegExp($tempJson, '(?i)fileUrl"\:"(.+?)"\}', 3)
If @error Then
$oHTTP = 0
Return
EndIf
;ConsoleWrite(StringReplace($temparr, '\', '') & @CRLF)
$fSource = _GetData($oHTTP, StringReplace($temparr, '\', ''))
_FileWrite($tempFilePath & $tempFileName, $fSource)
$oHTTP = 0
EndFunc ;==>_FileDown
Func _FileWrite($Path, $sData)
Local $hFileOpen = FileOpen(StringRegExpReplace($Path, '(.+?)(\..+)', '$1_' & _GetLocalTime() & '$2'), 1 + 16)
If $hFileOpen = -1 Then Return
FileWrite($hFileOpen, $sData)
FileClose($hFileOpen)
EndFunc ;==>_FileWrite
Func _GetData($oHTTP, $Url)
$oHTTP.Open('GET', $Url)
$oHTTP.SetRequestHeader('Accept-Language', 'zh-CN,zh;q=0.9')
$oHTTP.SetRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36')
$oHTTP.Send()
$oHTTP.WaitForResponse()
Return $oHTTP.ResponseBody
EndFunc ;==>_GetData
Func _GetLocalTime()
Local $tLocalTime = DllStructCreate('struct;word Year;word Month;word Dow;word Day;word Hour;word Minute;word Second;word MSeconds;endstruct')
DllCall('kernel32.dll', 'none', 'GetLocalTime', 'struct*', $tLocalTime)
Local $tLocalTimeValue = $tLocalTime.Year & StringRight('00' & $tLocalTime.Month, 2) & StringRight('00' & $tLocalTime.Day, 2) & StringRight('00' & $tLocalTime.Hour, 2) & StringRight('00' & $tLocalTime.Minute, 2) & StringRight('00' & $tLocalTime.Second, 2)
$tLocalTime = 0
Return $tLocalTimeValue
EndFunc ;==>_GetLocalTime
这种估计要带请求头,所以 InetGet() 不行 试过带头好像也不行,还是要用浏览器打开,会执行一个js脚本获取实际的下载连接 试过带了头 但是获取不到 Location
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Connection: keep-alive
DNT: 1
Host: develope-oss.lanzouc.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36
本帖最后由 afan 于 2024-6-29 11:30 编辑
那个Location 是响应头,不用管,只需看请求头。
我试过了,不带请求头不行,但只要带 Accept-Language: zh-CN 一项请求就可以下载到,下图就是测试下载的
afan 发表于 2024-6-29 11:26
那个Location 是响应头,不用管,只需看请求头。
我试过了,不带请求头不行,但只要带 Accept-Language: zh ...
能不能看看是用什么代码请求的 3131210 发表于 2024-6-29 20:20
能不能看看是用什么代码请求的
$oHTTP.SetRequestHeader("Accept-Language", "zh-CN")
页:
[1]