0

ストリームリーダーを使用して一部のページのHTMLを取得していますが、行がで始まる場合など、無視したい行があります<span> 

何かアドバイス?これが私の機能です

Public Function GetPageHTMLReaderNoPrx(ByVal address As Uri) As StreamReader
  Dim request As HttpWebRequest
  Dim response As HttpWebResponse = Nothing
  Dim reader As StreamReader

  Try
    request = DirectCast(WebRequest.Create(address), HttpWebRequest)
    response = DirectCast(request.GetResponse(), HttpWebResponse)

    Select Case CType(response, Net.HttpWebResponse).StatusCode
      Case 200
        reader = New StreamReader(response.GetResponseStream(), Encoding.Default)

      Case Else
        MsgBox(CType(response, Net.HttpWebResponse).StatusCode)
    End Select
  Catch
    If Not response Is Nothing Then response.Close()
  End Try
  Return reader
End Function

これはHTMLがどのように見えるかです

<tr>Text
<span>show all</span>
</tr>
4

1 に答える 1

1

文字列の使用を主張する場合は、次のようにすることができます。

Do
  Dim line As String = reader.ReadLine()
  If line Is Nothing Then Exit Do 'end of stream
  If line.StarsWith("<span>") Then Exit Do 'ignore this line
  'otherwise do some processing here
  '...
Loop

ただし、このアプローチは安定していません。入力HTMLに小さな変更を加えると、フローが中断する可能性があります。

よりエレガントな解決策は、以下を使用することXElementです。

Dim xml = <tr>Text
            <span>show all</span>
          </tr>
xml.<span>.Remove()
MsgBox(xml.Value.Trim)
于 2012-11-07T01:39:13.617 に答える