P粉5174756702023-09-03 18:13:40
IE 已死,不應再使用。但是,也可以使用縮短的網址https://cpc.farnell.com/SD02808< /a> 透過 xhr(XML HTTP 請求)。以下程式碼僅顯示如何從已載入的 HTML 程式碼中讀取所需資訊。所有保障措施均缺失。
上面顯示的程式碼中出現物件錯誤是因為您嘗試將資料類型String
的變數設為Nothing
,就像使用Set 的物件變數一樣strData =無
。
Sub GetCPCOfferInfos() Dim url As String Dim doc As Object Set doc = CreateObject("htmlFile") With CreateObject("MSXML2.XMLHTTP.6.0") url = "https://cpc.farnell.com/" & ActiveSheet.Cells(1, 1) .Open "GET", url, False .Send If .Status = 200 Then doc.body.innerHTML = .responseText ActiveSheet.Range("A3") = doc.getElementsByTagName("h1")(0).innertext _ & doc.getElementsByTagName("h2")(0).innertext 'Title ActiveSheet.Range("B3") = doc.getElementsByClassName("ManufacturerPartNumber")(0).innertext 'Manufacturer Part Number ActiveSheet.Range("C3") = doc.getElementById("pdpSection_FAndB").innertext 'Product Overview ActiveSheet.Range("D3") = doc.getElementById("pdpSection_pdpProdDetails").innertext 'Product Information Else MsgBox "Page not loaded. HTTP status " & .Status End If End With End Sub