从网站获取数据

我想从某个站点获取特殊数据,但我不知道如何。 该站点地址是: http://tsetmc.ir/Loader.aspx?ParTree=151311&i=35425587644337450 我想要的数据在这里: http://www.tsetmc.com/tsev2/data/instinfodata.aspx?i=35425587644337450&c=27+ 我需要一个vb.net源代码,该源代码可帮助每5秒从网络获取一次数据并将其拆分为表格。

我该如何实现?

flh12004029 回答:从网站获取数据

似乎网页上的数据已压缩,因此,如果您试图通过WebClient.DownloadStringWebClient.DownloadDataSystem.Text.Encoding.UTF8.GetString来获取数据,则不会可读的字符串。字符串可能看起来像这样

  

?y?]?u ?? j?7? s?H4 ?? N ??? {?vS(???? 7?N?±A?O?f ?? E ???-?O ?? q)?m,:K?:?{Ij.??J?Uem??-K?ni=KT???c?'?g??- ??] ?? A ??? a?> ??? o ??????? Ys ??> ?????? 5ga ?? Z [?v ?? s?F ????i ?eU?/?+,??!?f?9? t?2; bG ???(?? Y!?? oX ?? Gm ?? W ??????? Z ??? 8 ?????? =?y?WU ?? 9 ?? 7; z?^ ?????? T ?? Y?8] bih ?? | N?   ...

您可以看到响应的编码已压缩为以下内容

client.ResponseHeaders(HttpResponseHeader.ContentEncoding)
' equals gzip

这么简单

client.DownloadString(address)

将导致压缩的字符串。要考虑压缩,请使用System.IO.GZipStream进行解压缩。

此代码已完成。您要做的就是制作一个新表单,添加一个新标签,然后粘贴

Public Class Form1

    Private ReadOnly interval As Integer = 5000
    Private ReadOnly t As New System.Threading.Timer(AddressOf updateLabel,Nothing,-1,-1)
    Private ReadOnly address As String = "http://www.tsetmc.com/tsev2/data/instinfodata.aspx?i=35425587644337450&c=27+"

    Private Sub Form1_Load(sender As Object,e As EventArgs) Handles MyBase.Load
        t.Change(0,-1)
    End Sub

    Private Sub updateLabel(state As Object)
        Dim text As String
        Try
            text = getString(address)
            t.Change(interval,-1)
        Catch
            text = "error"
        End Try
        Label1.Invoke(Sub() Label1.Text = text)
    End Sub

    Private Shared Function getString(address As String) As String
        Dim text As String
        Using client As New Net.WebClient()
            Using stream = client.OpenRead(address)
                If client.ResponseHeaders(Net.HttpResponseHeader.ContentEncoding) = "gzip" Then
                    Using responseStream = New IO.Compression.GZipStream(stream,IO.Compression.CompressionMode.Decompress)
                        Using reader = New IO.StreamReader(responseStream)
                            text = reader.ReadToEnd()
                        End Using
                    End Using
                Else
                    Using reader = New IO.StreamReader(stream)
                        text = reader.ReadToEnd()
                    End Using
                End If
            End Using
        End Using
        Return text
    End Function

End Class

上面的代码允许压缩或非压缩响应。如果您知道它始终处于压缩状态,则可以使用此

Private Shared Function getString(address As String) As String
    Dim text As String
    Using client As New Net.WebClient()
        Using stream = client.OpenRead(address)
            Using responseStream = New IO.Compression.GZipStream(stream,IO.Compression.CompressionMode.Decompress)
                Using reader = New IO.StreamReader(responseStream)
                    text = reader.ReadToEnd()
                End Using
            End Using
        End Using
    End Using
    Return text
End Function

您现在应该获得可读的字符串

  

12:29:37,A,5254,5218,5203,5223,5277,5190,1727,16938744,88393224291,1,20191125,122937; 98/9/4 14:31:01,F,308477.15, 2703.04 0.88%,11256322598042802,2998595530,15357456521865,388202,F,986363991,12363278355103,190712,F,606761,179231120000,6176,; 4 @ 63890 @ 5230 @ 5254 @ 100000 @ 2,4 @ 120000 @ 5228 @ 5255 @ 6500 @ 1,2 @ 40000 @ 5222 @ 5259 @ 221500 @ 6,; 65589,406538,760335; 10352685,6586059,14312016,2626728,573,19,456,12 ;;; 0;

本文链接:https://www.f2er.com/3034274.html

大家都在问