更改页面后,使用vba从网页中提取文本

时间:2016-09-23 13:57:59

标签: html database excel vba web

我必须使用VBA从Web数据库中提取数据,为此我们可以放置一个页面Web(我们可以选择)并查看保存的数据(网页:appinvtinywebdb.appspot。 COM /的GetValue) 现在,我创建了一个VBA宏(下面的代码)来访问网页,在输入文本中写入正确的标签并单击提交按钮,我可以在Web导航器上看到数据(在字符串表单上)

问题是,当我想将字符串复制到VBA(解压缩)时,代码会从上一个网页(保留相同的URL)给出句子和字符串 我该如何阅读新页面?

Sub Lancer_Edoc()
Dim ie As New InternetExplorer
Dim MyStr As String

Set ie = New InternetExplorer

ie.Navigate2 "http://appinvtinywebdb.appspot.com/getvalue"

While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE
   DoEvents
Wend

ie.Visible = True

Dim htmldoc As HTMLDocument
Dim htmlForms As IHTMLElementCollection
Dim htmlForm As HTMLFormElement
Dim HtmlElementStandard As HTMLGenericElement
Dim LeTexteExtrait As String

' Set htmldoc = ie.document
' obtient collection des formulaire de la page
'Set htmlForms = htmldoc.getElementsByTagName("tag")
' référence le formulaire de login
'Set htmlForm = htmlForms.namedItem("frmLogin")
' remplit les cases
Set oDoc = ie.document
' Valeur recherchée
oDoc.getElementsByName("tag")(0).Value = "trait"
oDoc.getElementsByTagName("form")(0).submit
'Allons chercher ce texte dans notre Item46
Dim iet As New InternetExplorer

Set htmldoc = ie.document

Set HtmlElementStandard = htmldoc.body.all(0)

'On le place dans notre variable prévue à cet effet
LeTexteExtrait = HtmlElementStandard.innerText
'On affiche le texte
MsgBox LeTexteExtrait, Title:="Le texte extrait de la page"

End Sub

谢谢!! :)

1 个答案:

答案 0 :(得分:0)

直接连接而不是使用IE:

Private Sub ConnectAndSubmitValue

Dim oHTTP As MSXML2.ServerXMLHTTP60
Dim URL As String
Dim res As String
Dim resHTML As HTMLDocument
Dim YOURTEXT as string

YOURTEXT = EncodeUrl("I ENTERED THIS TEXT")

Set resHTML = New HTMLDocument
Set oHTTP = New MSXML2.ServerXMLHTTP60
    URL = "http://appinvtinywebdb.appspot.com/getvalue"
    res = "tag=" & YOURTEXT & "&fmt=html"

    With oHTTP
        .Open "POST", URL, False
        .SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .Send (res)

        res = .responseText
        resHTML.body.innerHTML = .responseText
    End With

End Sub

这将为您提供res作为整个html文档的字符串和resHTML html文档,以便您操作。