0

使用 Gambas,是否可以将网页下载到字符串,然后解析该字符串。我知道一旦我有了数据就可以解析字符串中的数据,我正在努力将网页中的数据转换成字符串。

4

1 回答 1

2

您可以使用gb.net.curl组件中的HttpClient

您还可以在此处找到如何同步或异步读取数据的示例。

要以字符串形式从 Web 获取数据,您可以编写以下函数(在这种情况下它将是同步的)

Public Function GetTextFromUrl(url As String) As String
    Dim client As New HttpClient As "client"

    client.URL = url
    client.async = False
    client.Get()

    ' an error occured
    If client.Status < 0 Then
        Return ""
    Endif

    ' no data available
    If Not Lof(client) Then
        Return ""
    Endif

    ' Reads the data from the server and returns it as a String
    Return Read #client, Lof(client)

End

你可以这样调用函数:

Public Sub Main()
    Print GetTextFromUrl("http://stackoverflow.com")
End
于 2016-07-24T21:33:14.683 回答