html - Excel VB 在网页上搜索文本并在同一元素中复制信息

标签 html excel vba

我是比较新的 VBA。

我正在尝试使用此代码从网站上获取一些信息。当我按 Element 执行此操作时,我必须搜索标记名称 tr 并使用它旁边的数字来定义我要使用的标记名称。问题是它经常随着网站上的位置而变化。目前我要搜索的关键字及其包含的信息是这样的:

<tr>
  <td class="nt">Operations</td>
  <td>Windows</td>
</tr>

因此,如果我可以按类“Operations”进行搜索,并获得信息“Windows”,那将会有所帮助。另外,我目前有一个错误

Next without For

如果可能的话,有没有办法在我关闭页面之前使用它进行多次搜索?所以我寻找多个特定的单词并将该数据输入到不同的单元格中,然后再移动到下一列,它会重复直到在 x 值结束时完成。我目前只将它设置为 x=2 到 5,但我想在未来将其增加到 10 或更高。

当前代码如下所示。

Private Sub Worksheet_Change(ByVal Target As Range)
  For x = 2 To 5
    If Target.Row = Cells(x, 35).Row And _
    Target.Column = Cells(x, 35).Column Then
    'If Target.Row = Range("ManufacturerPartNumber").Row And _
    'Target.Column = Range("ManufacturerPartNumber").Column Then
        Dim IE As New InternetExplorer
        'IE.Visible = True
        'For x = 2 To 5
        'IE.navigate "" & Range("Website_1").Value
        IE.navigate "" & Cells(x, 35).Value
        Do
            DoEvents
        Loop Until IE.readyState = READYSTATE_COMPLETE
        Dim Doc As HTMLDocument
        Set Doc = IE.document
        Dim sDD As String
        sDD = InStr(1, IE.document.body.innerHTML, "Processor Model")
        'sDD = Trim(Doc.getElementsByTagName("Processor Model")(1).innerText) 'Use this with tag like dd and number for which it appears like 0 or 1
        IE.Quit
        Dim aDD As Variant
        aDD = Split(sDD, ",")
        Cells(x, 44).Value = aDD(0)
        'Range("ProcessorNumberCd").Value = aDD(0)
        'Range("OSProvided").Value = aDD(0)
    Next x

    End If

  'MsgBox "Complete"
End Sub

最佳答案

我想你想捕获“内部文本”。请看下面的示例。

enter image description here

Sub Scraper()

   Dim item As Long
   Dim priceStr As String
   Dim priceTag As Object
   Dim priceTable As Object


   item = "10011" 'this will eventually be placed in a loop for multiple searches

   Set objIE = CreateObject("InternetExplorer.Application")
   objIE.Visible = True
   ' navigate and download the web page
   objIE.Navigate "www.google.com"
   Do While objIE.ReadyState <> 4 Or objIE.Busy
     DoEvents
   Loop

  'objIE.Document.getElementsByTagName("input")(0).Value = item
  'objIE.Document.getElementByID("FDI").Click

  Set priceTable = objIE.Document.getElementByID("price_FGC")
  Set priceTag = priceTable.getElementsByTagName("u")(3)
  priceStr = priceTag.innerText

  Sheet1.Range("A1").Value = priceStr

  objIE.Quit


  End Sub

此外,请查看此链接以了解如何执行其他类似操作的其他几种方法。

http://www.tushar-mehta.com/publish_train/xl_vba_cases/vba_web_pages_services/index.htm

关于html - Excel VB 在网页上搜索文本并在同一元素中复制信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40383973/

相关文章:

html - 在使用百分比和相对定位时,有没有办法防止我的流体设计被破坏?

html - 使用与 bean 不同的名称将 bean 值输入到 Struts (1.x) 中的隐藏输入中

javascript - JS中单词之间的空格

c++如何将数组导出到excel

sql-server - Excel VBA 和 ADODB : retrieve auto identity of row inserted into SQL Server from VBA

选择错误中每个单元格的 Excel VBA 函数导致变量未定义

html - 使用 CSS 定位 Flash 内容

vba - VBA中选择Case失败?

excel - 电量查询: bucket column into different slabs tiers

excel - 关闭前用代码关闭另一个工作簿错误