如何将网站文本的一部分抓取到 Excel 单元格中

How to grab a portion of a website text into an Excel cell

我正在尝试从 GM 零件网站自动创建一系列零件编号值的说明列表。

例如,以下是部件号 23498355 的 link - http://www.gmpartsdirect.com/oe-gm/23498355

我正在尝试将此网页上可用的部件描述文本 "This ABS Sensor is a genuine OEM GM part #23498355 and carries a factory warranty. We offer the best online prices with fast shipping on any order placed with us." 抓取到 Excel。

我编写了以下代码来获取该信息,但无法完成可以获取该特定信息的最后几行代码。

Option Explicit

Sub myConnection()
    Dim oHtml, myData, Title, cste
    Set oHtml = New HTMLDocument
    With CreateObject("WINHTTP.WinHTTPRequest.5.1")
        .Open "GET", "http://www.gmpartsdirect.com/oe-gm/23498355", False
        .send
        oHtml.body.innerHTML = .responseText
    End With
'Rest of the code to grab the exact part description
End Sub

一旦我开始工作,我的想法是自动执行零件号列表的过程。 谁能帮我完成这段代码?

使用 MSHTML 解析您的 HTML 有点受限,因为许多 "modern" 文档方法可能无法实现,但您可以在这种情况下使用它:

Sub myConnection()
    Dim oHtml, myData, Title, cste, d
    Set oHtml = New MSHTML.HTMLDocument


    With CreateObject("WINHTTP.WinHTTPRequest.5.1")
        .Open "GET", "http://www.gmpartsdirect.com/oe-gm/23498355", False
        .send
        oHtml.body.innerHTML = .responseText

        Set d = myGetElementsByClassName(oHtml, "div", "description_body")
        If Not d Is Nothing Then
            Debug.Print d.innerText
        End If

    End With
'Rest of the code to grab the exact part description
End Sub


'return an element given its tag name and class name
Function myGetElementsByClassName(doc, tagName, className) As Object
    Dim el As Object
    For Each el In doc.getElementsByTagName(tagName)
        If el.className = className Then
            Set myGetElementsByClassName = el
            Exit Function
        End If
    Next el
    Set myGetElementsByClassName = Nothing
End Function