簡體   English   中英

VB.net通過HTML代碼搜索

[英]VB.net searching through HTML code

我正在創建一個程序,該程序將搜索頁面HTML源代碼並返回是否存在指定的字符串,盡管該字符串總是返回false,有人可以看看我是否丟失了某些東西?

Private Const QUOTE As Char = """"c

Private Sub ServerStatus_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'download the page source and store it here
    Dim sourceString As String = New System.Net.WebClient().DownloadString("https://support.rockstargames.com/hc/en-us/articles/200426246")

    'call the source and validate a string exists, if not
    If (sourceString).Contains($"<div class={QUOTE}panel-base xbl{QUOTE} style={QUOTE}background-color: RGB(236, 255, 236);{QUOTE}><div class={QUOTE}marshmallowLogo{QUOTE} id={QUOTE}xboxLogo{QUOTE}>Xbox 360</div><center><span class={QUOTE}statusSpan{QUOTE} style={QUOTE}color green;{QUOTE}>Up</span></center>") = True Then
        Label1.Text = "It's there"
        ' if it does
    ElseIf (sourceString).Contains($"<div class={QUOTE}panel-base xbl{QUOTE} style={QUOTE}background-color: RGB(236, 255, 236);{QUOTE}><div class={QUOTE}marshmallowLogo{QUOTE} id={QUOTE}xboxLogo{QUOTE}>Xbox 360</div><center><span class={QUOTE}statusSpan{QUOTE} style={QUOTE}color green;{QUOTE}>Up</span></center>") = False Then
        Label1.Text = "It's not"
    End If

End Sub

末級

因此,我花了幾分鍾分析頁面(不客氣),如注釋中所述,數據是通過javascript加載的,並不存在於原始URL返回的基本html中。 我還不確定100%,但是我認為您實際上是想看看這個地址:

https://supportfiles.rockstargames.com/support/serverStatus.json

返回如下響應:

jsonCallbackStatus(
    {
        "statuses":

            {
                "psnUpOrDownOverride": "",
                "ps4UpOrDownOverride": "",
                "xboxUpOrDownOverride": "",
                "xboxOneUpOrDownOverride": "",
                "rgscUpOrDownOverride": "",
                "psnWarningOverrideMessage": "",
                "ps4WarningOverrideMessage": "",
                "xboxWarningOverrideMessage": "",
                "xboxOneWarningOverrideMessage": "",
                "rgscWarningOverrideMessage": "",
                "pcWarningOverrideMessage": "",
                "pcUpOrDownOverride": "",
                "giantWarningOverrideMessage": ""
            },

    }
);

如果我正確地閱讀了此內容,則每個項目旁邊的空字符串表示沒有問題,沒有新聞是好消息。 它應該比所有html都容易解析得多:)不要忘記查看平台的警告和啟動/關閉狀態以及giantWarningOverrideMessage

我如何找到這個地址

像這樣的數據幾乎總是以以下三種方式之一出現:json,rss(或類似的xml)或Web服務(soap)。 通常將在服務器上加載和解析Web服務,然后將其與html一起發送,而rss很難在javascript中進行解析,並且最近不太流行,因此我首先使用json。

我首先以chrome打開頁面。 然后,我打開開發人員工具( F12 ),然后選擇“ Network選項卡。 現在,當我刷新頁面時,將獲得從Web服務器為該頁面下載的每個項目的列表。 1然后,我僅通過查看javascript下載(工具欄中的JS按鈕……我正在尋找json響應)來縮小列表的范圍。 這給了我合理數量的項目,並且我可以通過僅查看200狀態響應來進一步縮小搜索范圍,其中我只看到兩個:都來自此地址。

請注意,完整地址實際上看起來像這樣:

https://supportfiles.rockstargames.com/support/serverStatus.json?callback=jsonCallbackStatus&callback=jsonCallbackStatus&_=1465445182216

頁面中存在一個錯誤,因為兩次callback URL參數毫無意義,尤其是使用相同的值時。 我只是因為_ url參數才提出這個問題。 將該值減去最后3位數字,最后得到一個恰好與今天的日期匹配的unix時間戳。 您可能想生成一個包含這樣的時間戳的url,因為Rockstar可能會在服務器上使用該時間戳以避免提供緩存的響應。 您不希望在一個小時前獲得響應緩存,如果服務器現在關閉,一切都很好,那么該響應將被緩存。

最后提醒一下:我不是100%確定這是您需要的數據。 它可能來自另一個請求。 但這就是您免費獲得的全部:)希望我能做到這一點的文章足以使您自己進行偵探工作,以驗證結果。

當然,您還可以選擇使用WebBrowser控件,該控件將運行javascript。 但它的方法要慢,你又回到了解析HTML難看,和任何一個小的HTML變化會破壞你的代碼(而JSON結果很可能通過幾個網站重新設計生活)。

讀取數據的源代碼

Dim unixTime As ULong = (DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliSeconds
Using wc As New WebClient(),
      rdr As New StreamReader(wc.OpenRead($"https://supportfiles.rockstargames.com/support/serverStatus.json?_={unixTime}"))

    Dim line = rdr.ReadLine()
    While line IsNot Nothing
        line = line.Trim()
        If line.StartsWith("""xboxUpOrDownOverride") Then
            Dim parts = line.Split(":".ToCharArray())
            parts(1) = Regex.Replace(parts(1), "[ "",]", "")
            If parts(1).Length > 0 Then
                Console.WriteLine("Up/Down Failed")
            Else
                Console.WriteLine("Up/Down Okay")
            End If
        End If
        If line.StartsWith("""xboxWarningOverrideMessage") Then
            Dim parts = line.Split(":".ToCharArray())
            parts(1) = Regex.Replace(parts(1), "[ "",]", "")
            If parts(1).Length > 0 Then
                Console.WriteLine("Warning Failed")
            Else
                Console.WriteLine("Warning Okay")
            End If
        End If
        If line.StartsWith("""giantWarningOverrideMessage") Then
            Dim parts = line.Split(":".ToCharArray())
            parts(1) = Regex.Replace(parts(1), "[ "",]", "")
            If parts(1).Length > 0 Then
                Console.WriteLine("Giant Warning Failed")
            Else
                Console.WriteLine("Giant Warning Okay")
            End If
        End If
        line = rdr.ReadLine()
    End While

您還應該考慮使用真正的json解析器(非常容易通過NuGet進行操作),因為即使添加最小化器之類的簡單操作也會通過將所有內容壓入一行來破壞現有代碼。


1並且下載了很多東西。 Rockstar應該投資捆綁軟件,以最大程度地減少HTTP請求,以加快頁面加載速度並降低帶寬,尤其是在移動設備上。

不能使用VS2015(VB14)的任何人的參考代碼:

Private Const QUOTE As Char = """"c

Private Sub ServerStatus_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'download the page source and store it here
    Dim sourceString As String = New System.Net.WebClient.DownloadString("https://support.rockstargames.com/hc/en-us/articles/200426246")

    'call the source and validate a string exists, if not

Label1.Text = If(sourceString.Contains(String.Format(
"<div class={0}panel-base xbl{0} style={0}background-color: RGB(236, 255, 236);{0}><div class={0}marshmallowLogo{0} id={0}xboxLogo{0}>Xbox 360</div><center><span class={0}statusSpan{0} style={0}color green;{0}>Up</span></center>",
QUOTE)),"It's there", "It's not")

    End If
End Sub
End Class

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM