0

私は困惑しています。アプリケーションで WebBrowser を使用したくありません。特定の要素を ID で取得したいと考えています。私のコードは次のとおりです。

Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://www.google.com/finance?q=NASDAQ:GOOG")
Dim response As System.Net.HttpWebResponse = request.getresponse()
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
Dim sourcecode As String = sr.ReadToEnd()
TextBox1.Text = sourcecode

これでソースコードが取得できます。しかし、どうすれば特定の要素を取得できますか? これを行う簡単な方法があると思います...ところで、正規表現を使用したり、HTML Agility Packをダウンロードしたりしたくありません。

4

2 に答える 2

0

id=elementname解析テーブルを作成して html タグを認識し、タグ内 (および可能な空白文字)を検索できます。ほとんどのタグを無視でき、html を検証する必要がないため、これは不可能なタスクではないように思われるかもしれません。<> だけを考えて、引用符やスクリプトなどの内容は無視してください。さらに多くの詳細があり、少し手間がかかりますが、プログラミングは楽しいものです。

別の方法としては、回避したい html アジリティ パックのようなものをダウンロードするか、ブラウザーを使用するか、正規表現を使用することです。

于 2013-01-15T05:30:43.233 に答える
0

これは非常に大まかなアイデアであり、SEPARATE 終了タグ ( など) を必要とする BLOCK 要素では機能しませんが、次のような自己終了要素では正常に機能します

また、タグ ID の中にはスピーチ マークで囲まれているものとそうでないものがあることにも注意しました。

このコードを大まかに編集して、囲まれていない id タグを検出するルーチンをコピー ペーストしましたが、まだ作業が必要で、短縮することもできます。

<script runat="server">
Dim sourcecode As String
Dim bodycode As String
Dim RetVal As String

Protected Sub Page_Load(sender As Object, e As System.EventArgs)
    '
    LoadHttpStuff()
    If Request.Form("Button1") = "Submit" Then
        RetVal = MyGetElementById(Request("Text1"))
    End If

End Sub

Private Sub LoadHttpStuff()

    Dim request As System.Net.HttpWebRequest
    Dim response As System.Net.HttpWebResponse
    Dim sr As System.IO.StreamReader
    Dim finishat As Long
    Dim startat As Long

    request = System.Net.HttpWebRequest.Create("http://www.google.com/finance?q=NASDAQ:GOOG")
    response = request.GetResponse()
    sr = New System.IO.StreamReader(response.GetResponseStream())
    sourcecode = sr.ReadToEnd()
    startat = InStr(sourcecode, "<body>")
    finishat = InStr(sourcecode, "</body>") + 7
    bodycode = Mid(sourcecode, startat, finishat - startat)
    bodycode = LCase(bodycode)


End Sub

Private Function MyGetElementById(Id As String) As String
    Dim tagstart As Long
    Dim tagend As Long
    Dim posx As Long
    Dim item As System.Web.UI.HtmlControls.HtmlGenericControl
    Dim test As Boolean
    Dim letter As Char
    Dim text As String
    item = Nothing
    test = False
    text = ""
    If Trim(Id) <> "" Then
        '-> with SPEECHMARKS
        posx = InStr(bodycode, LCase("id=" & Chr(34) & Id & Chr(34)))
        If posx > 0 Then
            'find start of tag
            Do
                posx = posx - 1
                letter = Mid(bodycode, posx, 1)
                If letter = "<" Then
                    'found tag start
                    tagstart = posx
                    Exit Do
                End If
            Loop Until posx < 1
            If tagstart > 0 Then
                posx = InStr(bodycode, LCase("id=" & Chr(34) & Id & Chr(34)))
                Do
                    posx = posx + 1
                    letter = Mid(bodycode, posx, 1)
                    If letter = ">" Then
                        tagend = posx + 1
                        Exit Do
                    End If
                Loop Until posx >= Len(bodycode)
                If tagend > 0 Then
                    text = Mid(bodycode, tagstart, tagend - tagstart)
                    test = True
                End If
            End If
        Else
            posx = InStr(bodycode, LCase("id=" & Id))
            If posx > 0 Then
                'find start of tag
                Do
                    posx = posx - 1
                    letter = Mid(bodycode, posx, 1)
                    If letter = "<" Then
                        'found tag start
                        tagstart = posx
                        Exit Do
                    End If
                Loop Until posx < 1
                If tagstart > 0 Then
                    posx = InStr(bodycode, LCase("id=" & Id))
                    Do
                        posx = posx + 1
                        letter = Mid(bodycode, posx, 1)
                        If letter = ">" Then
                            tagend = posx + 1
                        End If
                    Loop Until posx >= Len(bodycode)
                    If tagend > 0 Then
                        text = Mid(bodycode, tagstart, tagend - tagstart)
                        test = True
                    End If
                End If
            End If
        End If
    End If
    Return Text
End Function
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
    <table style="width: 100%;">
        <tr>
            <td style="text-align:left; vertical-align: top; width: 75%;"><textarea rows="20" cols="80" style="width: 90%;" disabled="disabled"><%=sourcecode%></textarea></td>
            <td style="width: 25%; text-align: left; vertical-align: top;">
                <table style="width:100%;">
                    <tr>
                        <td>Element Id&nbsp;<input id="Text1" name="Text1" type="text" /></td>
                    </tr><tr>
                        <td>&nbsp;</td>
                    </tr><tr>
                        <td>&nbsp;</td>
                    </tr><tr>
                        <td><input id="Button1" type="Submit" value="Submit" name="Button1" /></td>
                    </tr><tr>
                        <td>&nbsp;</td>
                    </tr><tr>
                        <td>&nbsp;</td>
                    </tr>
                </table>
            </td>
        </tr><tr>
            <td style="width: 75%;">&nbsp;</td>
            <td style="width: 25%;">&nbsp;</td>
        </tr><tr>
            <td style="width: 100%;" colspan="2"><textarea rows="20" cols="80" style="width: 75%;" disabled="disabled"><%=RetVal%></textarea></td>
            <td style="width: 25%;">&nbsp;</td>
        </tr>
    </table>
</form>
</body>
</html>

それが少し役立つことを願っています

于 2013-01-15T06:16:32.813 に答える