VB: 取网页源码

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值