Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long _
) As Long
Private Const CP_UTF8 = 65001
Private Declare Function MultiByteToWideChar Lib "kernel32" ( _
ByVal CodePage As Long, ByVal dwFlags As Long, _
ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _
ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
'取网页源码
Public Function webTxtGet(ByVal Url1 As String) As String
Dim strFile As String
Dim lngReturn As Long
Dim TextLine As String, textCon As String
Dim Big5str As String
Dim tmpUTF8() As Byte
Dim lLength As Long, lBytes As Long, lWC As Long
On Error Resume Next
strFile = App.Path & "/txtWeb.txt"
Open strFile For Output As #1
Close #1
lngReturn = URLDownloadToFile(0, Url1, strFile, 0, 0)
Open strFile For Binary As #1
webTxtGet = Space(LOF(1))
Get #1, , webTxtGet
Close #1
Kill strFile
End Function
'取网页源码_如果网页是UTF-8
Public Function webTxtGet_utf8(ByVal Url1 As String) As String
Dim strFile As String
Dim lngReturn As Long
Dim TextLine As String, textCon As String
Dim Big5str As String
Dim tmpUTF8() As Byte
Dim lLength As Long, lBytes As Long, lWC As Long
On Error Resume Next
strFile = App.Path & "/txtWeb.txt"
Open strFile For Output As #1
Close #1
lngReturn = URLDownloadToFile(0, Url1, strFile, 0, 0)
ReDim tmpUTF8(FileLen(strFile) - 1)
Open strFile For Binary As #1
Get #1, , tmpUTF8
Close #1
'utf8->big5转码
lBytes = UBound(tmpUTF8) - LBound(tmpUTF8) + 1
lWC = lBytes
Big5str = String$(lBytes, Chr(0))
lLength = MultiByteToWideChar(CP_UTF8, 0, VarPtr(tmpUTF8(0)), lBytes, _
StrPtr(Big5str), lWC)
Big5str = Left$(Big5str, lLength)
webTxtGet_utf8 = Big5str
Kill strFile
End Function