0

現在、特定の Web ページからデータを取得するアプリケーションを開発しようとしています。

この Web ページに次のコンテンツがあるとします。

<needle1>HAYSTACK 1<needle2>
<needle1>HAYSTACK 2<needle2>
<needle1>HAYSTACK 3<needle2>
<needle1>HAYSTACK 4<needle2>
<needle1>HAYSTACK 5<needle2>

そして、次の VB.NET コードがあります。

Dim webClient As New System.Net.WebClient
Dim FullPage As String = webClient.DownloadString("PAGE URL HERE")
Dim ExtractedInfo As String = GetBetween(FullPage, "<needle1>", "<needle2>")

GetBetween は次の関数です。

Function GetBetween(ByVal haystack As String, ByVal needle As String, ByVal needle_two As String) As String
    Dim istart As Integer = InStr(haystack, needle)
    If istart > 0 Then
        Dim istop As Integer = InStr(istart, haystack, needle_two)
        If istop > 0 Then
            Dim value As String = haystack.Substring(istart + Len(needle) - 1, istop - istart - Len(needle))
            Return value
        End If
    End If
    Return Nothing
End Function

上記のコードを使用すると、ExtractedInfo は常に "HAYSTACK 1" と等しくなります。これは、最初に検出されたものから常に干し草の山を取得するためです。

私の質問は次のとおりです。2番目、3番目、4番目などの出現を探すために、ある種の配列のようにExtractedInfoをセットアップする方法。

何かのようなもの:

ExtractedInfo(1) = HAYSTACK 1
ExtractedInfo(2) = HAYSTACK 2

前もって感謝します!

4

1 に答える 1

1

編集:これはあなたが実際に求めていたものだと思います。「針」のセットごとに、GetBetween 関数を 1 回呼び出します。

Dim webClient As New System.Net.WebClient
Dim FullPage As String = webClient.DownloadString("PAGE URL HERE")
Dim ExtractedInfo As List (Of String) = GetBetween(FullPage, "<needle1>", "<needle2>")

Function GetBetween(ByVal haystack As String, ByVal needle As String, ByVal needle2 As String) As List(Of String)
        Dim result As New List(Of String)
        Dim split1 As String() = Split(haystack, needle).ToArray
        For Each item In split1
            Dim split2 As String() = Split(item, needle2)
            Dim include As Boolean = True
            For Each element In split2
                If include Then
                    If String.IsNullOrWhiteSpace(element) = False Then result.Add(element)
                End If
                include = Not include
            Next element
        Next item

        Return result
End Function
于 2013-05-19T15:05:35.957 に答える