【问题标题】:Return entire page text from IE object从 IE 对象返回整个页面文本
【发布时间】:2017-06-25 21:29:00
【问题描述】:

我正在使用带有 VBA 的正则表达式来接收网页上的电子邮件,所有这些电子邮件的格式都非常不同。由于格式上的这些差异,我很难访问整个页面文本。

目前我的方法只是使用

Dim retStr as String
retStr = ie.document.body.innerText

ie 来自Set ie = CreateObject("InternetExplorer.Application")

看起来很简单,但在某些页面上,例如this one,并非所有页面文本都被返回。 “所有页面文本”,我的意思是任何 ctrl+f 会起作用的东西。在链接页面中,似乎没有返回每个“步骤”的文本。我想不同的网页之间会有差异,特别是如果它们不是 HTML 格式的。

在网页上按 ctrl+a 会返回我想要的文本,有什么方法可以在不使用 sendkeys 的情况下访问此文本吗?

【问题讨论】:

  • 您是否尝试使用即时窗口或本地窗口来检查从ie.document.body.innerText 返回的字符串?
  • @RobinMackenzie,是的,刚刚发现了调试窗口中无法容纳的错误

标签: html vba excel internet-explorer


【解决方案1】:

它对我来说工作得很好。我感觉您正在将其写入 Excel 单元格,因此文本被截断。

我把它写到一个文本文件中,我得到了完整的文本。

Sub Sample()
    Dim ie As Object
    Dim retStr As String

    Set ie = CreateObject("internetexplorer.application")

    With ie
        .Navigate "http://www.wikihow.com/Choose-an-Email-Address"
        .Visible = True
    End With

    Do While ie.readystate <> 4: Wait 5: Loop

    DoEvents

    retStr = ie.document.body.innerText

    '~> Write the above to a text file
    Dim filesize As Integer
    Dim FlName As String

    '~~> Change this to the relevant path
    FlName = "C:\Users\Siddharth\Desktop\Sample.Txt"

    filesize = FreeFile()

    Open FlName For Output As #filesize

    Print #filesize, retStr
    Close #filesize
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 2011-02-04
    • 1970-01-01
    • 2016-02-20
    • 2016-05-18
    相关资源
    最近更新 更多