簡體   English   中英

如何將網站文本的一部分捕獲到Excel單元格中

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

我正在嘗試自動從GM零件網站創建描述列表,以獲取一系列零件號值。

例如,下面是鏈接部件編號23498355 - http://www.gmpartsdirect.com/oe-gm/23498355

我試圖抓住零件說明文本“此ABS傳感器是OEM GM的真正零件#23498355,並具有工廠保修。我們提供的在線價格最優惠,而且在與我們的任何訂單中均可快速發貨。” 可在此網頁上導入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會受到一些限制,但是在這種情況下,您可以使其工作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM