找回密码
 加入
搜索
查看: 345|回复: 7

[AU3基础] 【已解决】用au3的InetGet下载文件错误

[复制链接]
发表于 2024-6-27 14:54:47 | 显示全部楼层 |阅读模式
本帖最后由 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[1] & '&pass=' & $sFileUrlSplit[2], 1 + 2))
        ConsoleWrite($tempJson & @CRLF)
        $temparr = StringRegExp(BinaryToString($tempJson), '(?i)fileName.+?\:"(.+?)".+?fileId"\:"(.+?)"\}', 3)
        If @error Then Return
        $tempFileName = $temparr[0]
        FileDelete($tempFilePath & $tempFileName)
        If FileExists($tempFilePath & $tempFileName) Then Return ;无法删除文件
        $tempJson = BinaryToString(InetRead('https://api.huankong.top/lanzou/?fileId=' & $temparr[1], 1 + 2))
        ConsoleWrite($tempJson & @CRLF)
        $temparr = StringRegExp(BinaryToString($tempJson), '(?i)fileUrl"\:"(.+?)"\}', 3)
        If @error Then Return
        ConsoleWrite(StringReplace($temparr[0], '\', '') & @CRLF)
        Local $hDownload = InetGet(StringReplace($temparr[0], '\', ''), $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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2024-6-29 21:14:15 | 显示全部楼层
本帖最后由 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[1] & '&pass=' & $sFileUrlSplit[2]), 1)
        ;ConsoleWrite($tempJson & @CRLF)
        $temparr = StringRegExp($tempJson, '(?i)^.+?fileName.+?\:"(.+?)".+?fileId"\:"(.+?)"\}', 3)
        If @error Then
                $oHTTP = 0
                Return
        EndIf
        $tempFileName = $temparr[0]
        $tempJson = BinaryToString(_GetData($oHTTP, 'https://api.huankong.top/lanzou/?fileId=' & $temparr[1]), 1)
        ;ConsoleWrite($tempJson & @CRLF)
        $temparr = StringRegExp($tempJson, '(?i)fileUrl"\:"(.+?)"\}', 3)
        If @error Then
                $oHTTP = 0
                Return
        EndIf
        ;ConsoleWrite(StringReplace($temparr[0], '\', '') & @CRLF)
        $fSource = _GetData($oHTTP, StringReplace($temparr[0], '\', ''))
        _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

发表于 2024-6-28 19:00:15 | 显示全部楼层
这种估计要带请求头,所以 InetGet() 不行
 楼主| 发表于 2024-6-29 02:10:54 | 显示全部楼层
试过带头好像也不行,还是要用浏览器打开,会执行一个js脚本获取实际的下载连接
 楼主| 发表于 2024-6-29 10:13:58 | 显示全部楼层
试过带了头 但是获取不到 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




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2024-6-29 11:26:56 | 显示全部楼层
本帖最后由 afan 于 2024-6-29 11:30 编辑

那个Location 是响应头,不用管,只需看请求头。
我试过了,不带请求头不行,但只要带 Accept-Language: zh-CN 一项请求就可以下载到,下图就是测试下载的

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2024-6-29 20:20:01 | 显示全部楼层
afan 发表于 2024-6-29 11:26
那个Location 是响应头,不用管,只需看请求头。
我试过了,不带请求头不行,但只要带 Accept-Language: zh ...

能不能看看是用什么代码请求的
发表于 2024-6-29 20:52:47 | 显示全部楼层
3131210 发表于 2024-6-29 20:20
能不能看看是用什么代码请求的

$oHTTP.SetRequestHeader("Accept-Language", "zh-CN")
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-8 10:05 , Processed in 0.098454 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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