在维基百科页面上找到类别

时间:2020-07-21 13:21:09

标签: vb.net search

简而言之,我要完成一项任务,我需要读取随机维基百科页面的标题以及其中的所有类别

我成功地(但是效率很低)设法通过在页面外调用html并搜索标题来找到标题,但是我不知道如何找到类别...有什么想法吗?

Sub Main()
        'this holds the webadress
         Dim webadress As String = "https://en.wikipedia.org/wiki/Seventh_Day_Baptists"
        'this causes the loop to keep going until title of page has been located
        Dim titlefound As Boolean = False
        'reads the entire html off of the wikepedia page
        Dim sourceString As String = New System.Net.WebClient().DownloadString(webadress)
        'holds the position at which the title is within the html
        Dim position As Integer
        'used to compile the title out of the html
        Dim title As String
        'loops until title found
        While titlefound <> True
            'used to flick through each character of html(aware its inefficent but easiest way I could think of)
            For i = 0 To sourceString.Length()
            'checks if its reading title
                If sourceString(i) = "t" And sourceString(i + 1) = "i" And sourceString(i + 2) = "t" And sourceString(i + 3) = "l" And sourceString(i + 4) = "e" Then
                    Console.WriteLine("locating title...")
                    Console.WriteLine("found")
                    position = i + 6
                    titlefound = True
                    Exit For
                End If
            Next


        End While
    'searches for the hyphen after the title to determin when the title stops
        For i = position To 100000
            If sourceString(i) = "-" Then
                Exit For
            End If
            title = title & sourceString(i)

        Next
'displays the title
        Console.Write("the title is: ")
        Console.ForegroundColor = ConsoleColor.Red
        console.writeline(title)

    End Sub

0 个答案:

没有答案
相关问题