我正在尝试自动化从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