【发布时间】:2022-08-14 04:07:40
【问题描述】:
我正在尝试提取公司名称从一个网站。
我收到一个错误。
e]1
在For iCnt = 0 To .getElementsByTagName(\"h2\").Length - 1
我需要提取网站上提供的所有详细信息,例如:
Business Name
Address
Telephone
Fax
Email
Website
按顺序排列,以便所有详细信息都可以粘贴到 Excel 文件中。
Option Explicit
Const sSiteName = \"https://www.thoroughexamination.org/postcode-search/nationwide?page=1\"
Private Sub getHTMLContents()
\' Create Internet Explorer object.
Dim IE As Object
Set IE = CreateObject(\"InternetExplorer.Application\")
IE.Visible = False \' Keep this hidden.
IE.Navigate sSiteName
\' Wait till IE is fully loaded.
While IE.ReadyState <> 4
DoEvents
Wend
Dim oHDoc As HTMLDocument \' Create document object.
Set oHDoc = IE.Document
Dim oHEle As HTMLUListElement \' Create HTML element (<ul>) object.
Set oHEle = oHDoc.getElementById(\"search-item-header\") \' Get the element reference using its ID.
Dim iCnt As Integer
\' Loop through elements inside the <ul> element and find <h1>, which has the texts we want.
With oHEle
For iCnt = 0 To .getElementsByTagName(\"h2\").Length - 1
Debug.Print .getElementsByTagName(\"h2\").Item(iCnt).getElementsByTagName(\"a\").Item(0).innerHTML
Next iCnt
End With
\' Clean up.
IE.Quit
Set IE = Nothing
Set oHEle = Nothing
Set oHDoc = Nothing
End Sub
第二个代码没有响应:
Sub TutorailsPoint()
Const URL = \"https://www.thoroughexamination.org/postcode-search/nationwide?page=1\"
Dim http As New MSXML2.XMLHTTP60, html As New HTMLDocument
Dim topics As Object, posts As Object, topic As Object
Dim x As Long
x = 1
http.Open \"GET\", URL, False
http.send
html.body.innerHTML = http.responseText
Set topics = html.getElementsByClassName(\"search-item-header\")
For Each posts In topics
For Each topic In posts.getElementsByTagName(\"h2\")
Cells(x, 1) = topic.innerText
x = x + 1
Next topic
Next posts
End Sub
标签: excel vba web-scraping