【问题标题】:Parse data from internet HTML page using VBA and IE使用 VBA 和 IE 从 Internet HTML 页面解析数据
【发布时间】:2020-01-09 08:44:05
【问题描述】:

披露:我只是一名办公室文员,对 VBA 和 HTML 非常陌生。希望你对我有耐心。我非常感谢任何指导和帮助。希望我的格式正确。

昨天我花了一整天时间尝试从 Intranet 网页导入信息以自动复制和粘贴路由。从长远来看,这确实会有所帮助。

由于 power-query 似乎没有看到我需要的表,我认为唯一的选择是使用 VBA。 MsServer 工具完美抓取页面,但令我失望的是页面出现错误,因为它需要先授权才能访问它。

我认为使用 IE 应该可以工作,因为 IE 在 cookie 中有登录信息。

到这里为止。

 Sub ExtractFromEndeca() Dim ie As InternetExplorer Dim html As
 IHTMLDocument Set ie = CreateObject("InternetExplorer.Application")
 ie.Visible = False 
 ie.Navigate "intranet address"
 While ie.Busy
     DoEvents Wend While ie.ReadyState < 4
     DoEvents Wend
     Set Doc = CreateObject("htmlfile")
     Set Doc = ie.document
     Set Data = Doc.getElementById("findSimilarOptions2")
     Sheet1.Cells(1, 1) = Data
     ie.Quit Set ie = Nothing

 ThisWorkbook.Sheets(1).Cells(1, 1) = Data

 End Sub

结果是单元格 A1 中的 [object],仅此而已,我无法理解我是否已通过登录。

这是我要抓取的页面片段。理想情况下,这些数据将作为表格输出。

   <td valign="top" id="findSimilarOptions2">
<div class="subtitle">Part Attributes</div>
    <input type="checkbox" id="n_200012" value="-19192896" NAME="n_200012">
    <b>
    ASSY TYPE</b>&nbsp;>
    Component<br>

    <input type="checkbox" id="n_200013" value="-18148519" NAME="n_200013">
    <b>
    PARAMETER I NEED(1)</b>&nbsp;>
    VALUE I NEED(1)<br>

    <input type="checkbox" id="n_200006" value="-20823731" NAME="n_200006">
    <b>
    PARAMETER I NEED(2)</b>&nbsp;>
    VALUE I NEED(2)<br>

    <input type="checkbox" id="n_200006" value="-20823618" NAME="n_200006">
    <b>
    PARAMETER I NEED(3)</b>&nbsp;>
    VALUE I NEED(3)<br>

    <input type="checkbox" id="n_200006" value="-20823586" NAME="n_200006">
    <b>
    PARAMETER I NEED(4)</b>&nbsp;>
    VALUE I NEED(4)<br>
    ...

【问题讨论】:

  • 欢迎来到 SO。如果您使用Sheet1.Cells(1, 1) = Data.Value 而不是Sheet1.Cells(1, 1) = Data,会发生什么?
  • 啊,我想我知道为什么了。 &lt;td valign="top" id="findSimilarOptions2"&gt; 没有任何值,因此无法将其返回到您的 Excel 文件中。但是,您的 HTML 代码的其他元素应该可以工作,例如&lt;input type="checkbox" id="n_200012" value="-19192896" NAME="n_200012"&gt;。尝试测试Set Data = Doc.getElementById("n_200012"),然后测试Sheet1.Cells(1, 1) = Data.Value
  • @JustynaMK 是的,它获取值 -19192896!谢谢你。但这不是我需要的。我需要 id "findSimilarOptions2" 的内部文本
  • @JustynaMK UPD:我刚刚测试过,它使用 data.innerText 抓取它!我太兴奋了,我终于到了一个我忘了去吃午饭的地方。我将尝试合并下面的答案,以帮助在午餐后构建这些数据。再次非常感谢。
  • 非常积极的消息!很高兴你进展顺利。我很清楚你的感受,但请不要忘记吃东西 :-) 保重。

标签: excel vba web-scraping


【解决方案1】:

请在下面的代码中阅读我的cmets:

'Use the following line in every module head
'It forces you to define all variables
Option Explicit

Sub ExtractFromEndeca()

Dim ie As InternetExplorer
Dim doc As IHTMLDocument 'You don't use html in your code, but doc
Dim data As HTMLHtmlElement 'You should define all variables
Dim singleData As HTMLHtmlElement 'New variable
Dim row As Long 'New variable

  row = 1 'First row for output in Excel table

  'Set ie = CreateObject("InternetExplorer.Application") 'This could be problematic on the intranet due to security guidelines
  Set ie = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}") 'Try this instead to initialize the IE
  ie.Visible = True 'This property should be True while development
  ie.Navigate "intranet address"
  'While ie.Busy: DoEvents: Wend 'You don't need this line
  While ie.ReadyState <> 4: DoEvents: Wend
  'Set Doc = CreateObject("htmlfile") 'You don't need this line
  Set doc = ie.document
  Set data = doc.getElementById("findSimilarOptions2").getElementsByTagName("input")

  'Data is only a reference to an object
  'You want the text information which lies in the value attributes of each input tag
  For Each singleData In data
    Sheet1.Cells(row, 1) = data.Value
    row = row + 1
  Next singleData

  'Clean up
  '(Automatic after development has finished)
  'ie.Quit
  'Set ie = Nothing
End Sub

【讨论】:

  • 您好,非常感谢。这变得非常清楚。它会打开新的 IE 窗口,但是会在此行中产生错误 Set data = doc.getElementById("findSimilarOptions2")(0).getElementsByTagName("input")。错误是 424:需要对象。是不是表示没有通过登录?
  • @AndreyRassanov 对不起,我的错。 getElementByID() 不需要 (0) 来获取节点集合的特定索引元素,因为 id 在 html 文档中应该只有一次。这就是为什么使用 getElementbyID() 没有节点集合的原因。我编辑了这条线。
  • 它产生错误'13'。在同一行中键入不匹配。我是否理解正确,您正在尝试获取 标签?我需要的是这些标签之间的文本。如果我理解正确,标签输入是自包含的,首先不包含此文本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 2010-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多