彻底解决VB.NET获取网页源代码的问题

来源:互联网 发布:python in action 编辑:程序博客网 时间:2024/05/17 02:23

在解决这个问题之前,我一直很苦恼。在网上到处搜寻,都找不到相应的解决办法。网上,虽然有提及完全相同的问题的帖子,但是答案最后往往没有得到发帖人的肯定,或者帖子就此die在论坛中。为了解决我设计网络爬虫程序所不可回避的获取网页编码的问题,我克服重重困难,在MSDN的一个帖子中的一个高人的一句话点醒了我。我似乎已经找到了解决问题的办法,但是后来,发现找到的那种办法的确可以解决乱码的问题,但是会出现获取的网页不完整的问题。

在我坚韧不拔的意志的支持下,我不停的尝试,不停的改进,终于设计出了一个可以完全解决此问题的代码,在这里和大家分享,并声明:不可用于商业目的,仅用于爱好者学习思想。相信大家可以设计出更加好,更加高效的程序。

 Function GetWebCode(ByVal strURL As String) As String        Dim httpReq As System.Net.HttpWebRequest        Dim httpResp As System.Net.HttpWebResponse        Dim httpURL As New System.Uri(strURL)        Dim ioS As System.IO.Stream, charSet As String, tCode As String        Dim k() As Byte        ReDim k(0)        Dim dataQue As New Queue(Of Byte)        httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)        Dim sTime As Date = CDate("1990-09-21 00:00:00")        httpReq.IfModifiedSince = sTime        httpReq.Method = "GET"        httpReq.Timeout = 7000        Try            httpResp = CType(httpReq.GetResponse(), HttpWebResponse)        Catch            Debug.Print("weberror")            GetWebCode = "<title>no thing found</title>" : Exit Function        End Try        '以上为网络数据获取        ioS = CType(httpResp.GetResponseStream, Stream)        Do While ioS.CanRead = True            Try                dataQue.Enqueue(ioS.ReadByte)            Catch                Debug.Print("read error")                Exit Do            End Try        Loop        ReDim k(dataQue.Count - 1)        For j As Integer = 0 To dataQue.Count - 1            k(j) = dataQue.Dequeue        Next            '以上,为获取流中的的二进制数据        tCode = Encoding.GetEncoding("UTF-8").GetString(k) '获取特定编码下的情况,毕竟UTF-8支持英文正常的显示        charSet = Replace(GetByDiv2(tCode, "charset=", """"), """", "") '进行编码类型识别            '以上,获取编码类型        If charSet = "" Then 'defalt            If httpResp.CharacterSet = "" Then                tCode = Encoding.GetEncoding("UTF-8").GetString(k)            Else                tCode = Encoding.GetEncoding(httpResp.CharacterSet).GetString(k)            End If        Else            tCode = Encoding.GetEncoding(charSet).GetString(k)        End If        Debug.Print(charSet)        'Stop            '以上,按照获得的编码类型进行数据转换            '将得到的内容进行最后处理,比如判断是不是有出现字符串为空的情况            GetWebCode = tCode            If tCode = "" Then GetWebCode = "<title>no thing found</title>"    End Function
    Function GetByDiv2(ByVal code As String, ByVal divBegin As String, ByVal divEnd As String)  '获取分隔符所夹的内容[完成,未测试]        '仅用于获取编码数据        Dim lgStart As Integer        Dim lens As Integer        Dim lgEnd As Integer        lens = Len(divBegin)        If InStr(1, code, divBegin) = 0 Then GetByDiv2 = "" : Exit Function        lgStart = InStr(1, code, divBegin) + CInt(lens)        lgEnd = InStr(lgStart + 1, code, divEnd)        If lgEnd = 0 Then GetByDiv2 = "" : Exit Function        GetByDiv2 = Mid(code, lgStart, lgEnd - lgStart)    End Function

将如上代码复制,并引用:

Imports System.Net
Imports System.IO
Imports System.Text.Encoding
Imports System.Text

然后,就可以使用这个代码完成网页源代码下载的工作了。

欢迎提出改进意见看法。

原创粉丝点击