Get data from a site

陌路散爱 提交于 2020-01-14 06:54:09

问题


I want to get special data from a site but i don't know how.

I need a vb.net source code that help to get this data from web every 5 seconds and split them in a form.

How can I achieve this?


回答1:


It looks like the data on the webpage is compressed, so if you had been trying to get the data via WebClient.DownloadString, or WebClient.DownloadData, System.Text.Encoding.UTF8.GetString, then you would not get a readable string. The string might look like this

?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? ...

You can see that the encoding of the response is compressed with the following

client.ResponseHeaders(HttpResponseHeader.ContentEncoding)
' equals gzip

so doing simply

client.DownloadString(address)

will result in the compressed string. To account for compression, use System.IO.GZipStream to decompress.

This code is complete. All you do is make a new Form, add a new Label, and paste this

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

The code above allows either compressed or non-compressed responses. If you know it's always compressed, you can just use this

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

You should now be getting a readable string

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,0,14312016,2626728,573,19,0,456,12;;;0;

|improve this answer

来源:https://stackoverflow.com/questions/59037737/get-data-from-a-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!