vb.net利用nsoup读取5173上面的网页数据

Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Windows.Controls

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim S = GetWebCode("http://s.5173.com/jxqy-5ootfk-0-0-0-kb0ewi-0-0-0-a-a-a-a-a-0-itemprice_asc-0-0.shtml")
        Dim doc = NSoup.NSoupClient.Parse(S)
        For Each I In doc.Select("#dlga").Select("a")
            Dim temp = New ListViewItem(I.Attr("title").ToString)
            temp.Tag = I.Attr("href")
            ListView1.Items.Add(temp)
        Next
    End Sub

    Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
        If ListView1.SelectedItems().Count >= 1 Then

            ListView2.Items.Clear()
            Dim uri = ListView1.SelectedItems(0).Tag
            Dim doc = NSoup.NSoupClient.Parse(GetWebCode(uri))
            For Each i In doc.Select("#dlgs").Select("a")
                Dim temp = New ListViewItem(i.Attr("title").ToString)
                temp.Tag = i.Attr("href")
                ListView2.Items.Add(temp)
            Next
        End If
    End Sub

    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

    Private Sub ListView2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView2.SelectedIndexChanged
        If ListView2.SelectedItems().Count >= 1 Then

            ListView3.Items.Clear()
            Dim uri = ListView1.SelectedItems(0).Tag
            Dim S = NSoup.NSoupClient.Parse(GetWebCode(uri))
            For Each I In S.Select(".sin_pdlbox")
                Dim jiage = I.Select(".pdlist_price").Text
                Dim bili = I.Select(".pdlist_unitprice").Select("b").Text
                Dim shuliang = I.Select(".pdlist_num").Text
                Dim biliang = I.Select(".pdlist_info").Select(".tt").Text.Split("=")(0)
                Dim dizhi = I.Select(".pdlist_info").Select("a").Attr("href")
                ListView3.Items.Add(New ListViewItem({bili, jiage, shuliang, biliang, dizhi}))
            Next
        End If
    End Sub

    Private Sub ListView3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView3.SelectedIndexChanged
        If ListView3.SelectedItems().Count >= 1 Then
            Try
                Process.Start(ListView3.SelectedItems(0).SubItems(4).Text)
            Catch ex As Exception
            End Try
        End If
    End Sub
End Class

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值