|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
下载这个验证码到本地,现金答谢好心人
url = "https://amr.sz.gov.cn/outer/validate/getCode.do?timestamp=" & Rnd()
- Public Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Integer, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Integer, ByVal lpfnCB As Integer) As Long
- 'https://amr.sz.gov.cn/outer/entSelect/gs.html
- Sub Djpg1()
- Dim Url As String
- Dim Path As String
- Path = ThisWorkbook.Path: ChDrive (Path): ChDir (Path)
- Path = Path & "\xcode.jpg"
- Url = "https://amr.sz.gov.cn/outer/validate/getCode.do?timestamp=" & Rnd()
- URLDownloadToFile 0, Url, Path & "\xcode.jpg", 0, 0
- End Sub
- '===========================================================
- '====================方法1下载不成功
- '===========================================================
- 'url = "https://amr.sz.gov.cn/outer/validate/getCode.do?timestamp=" & Rnd()
- Sub Djpg2()
- Dim Url As String
- Dim FilePath As String
-
- Url = "https://amr.sz.gov.cn/outer/validate/getCode.do?timestamp=1576573885"
- FilePath = ThisWorkbook.Path: ChDrive (FilePath): ChDir (FilePath)
- FilePath = FilePath & "\xcode.jpg"
-
- DownloadImage Url, FilePath
- End Sub
- Function DownloadImage(Url As String, FilePath As String) As Boolean
- Dim httpReq As Object
- On Error GoTo ErrorHandler
- Set httpReq = CreateObject("MSXML2.XMLHTTP")
- With httpReq
- .Open "GET", Url, False
- .Send
- End With
- If httpReq.Status = 200 Then
- With CreateObject("ADODB.Stream")
- .Open
- .Type = 1
- .Write httpReq.ResponseBody
- .SaveToFile FilePath, 2
- .Close
- End With
- DownloadImage = True
- Else
- DownloadImage = False
- End If
- Exit Function
- ErrorHandler:
- DownloadImage = False
- End Function
- '===========================================================
- '====================方法2下载不成功
- '===========================================================
复制代码
|
|