我想检索html页面的<h3>
标签的href属性,我能够获取innerText,但是我不知道如何访问href属性,有几个{{1 }}标签,但暂时我只需要第一个。稍后我会处理其余的...
这是我到目前为止获得的代码
<h3>
以下是HTML文档的一部分,用于从以下位置提取数据:
Sub Scrap()
Dim IE As New InternetExplorer
Dim sDD As String
Dim Doc As HTMLDocument
IE.Visible = True
IE.navigate "https://www.oneoiljobsearch.com/senior-reservoir-engineer-jobs/?page=1"
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Set Doc = IE.document
sDD = Trim(Doc.getElementsByTagName("h3")(0).innerText)
'sDD contains the string "Senior Reservoir Engineer"
End Sub
我需要检索的文本是:“ / jobs / senior-reservoir-engineer-oslo-norway-7?cmp = js&from = job-search-form-2”
在此先感谢您的帮助。
答案 0 :(得分:1)
尝试
dim hr as string
hr = Doc.getElementsByTagName("h3")(0).getElementsByTagName("a")(0).href
debug.print hr
getElementsByTagName集合从零开始,而.Length(H3的数量,在其他方法中称为Count)是从一开始。
dim i as long
for i=0 to Doc.getElementsByTagName("h3").length - 1
debug.print Doc.getElementsByTagName("h3")(i).getElementsByTagName("a")(0).href
next i
这将从每个H3获取第一个标签。您可以复制该方法,以从每个H3中获取多个A。
答案 1 :(得分:0)
在最终代码下面,以防它对某人有所帮助...
Sub MultiScrap()
Dim IE As New InternetExplorer
Dim hr As String
Dim Doc As HTMLDocument
Dim i, j, s As Long
Sheets("LNK0").Activate
myHTTP = Cells(1, 2) 'http address root
lval = Cells(2, 2) 'min number to add to root (page=1..)
uval = Cells(3, 2) 'max number to add to root (page=10..)
s = 5
For i = lval To uval 'loop through all pages
'IE.Visible = True
IE.navigate myHTTP & i
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Set Doc = IE.document
For j = 0 To Doc.getElementsByTagName("h3").Length - 1
Cells(s, 1) = s - 4 'Correl
Cells(s, 2) = i 'Page
Cells(s, 3) = j 'Row in page
Cells(s, 4) = Doc.getElementsByTagName("h3")(j).getElementsByTagName("a")(0).href 'Http
hyperAddres = Cells(s, 4).Value
hyperTxt = Cells(s, 4).Value
Cells(s, 4).Hyperlinks.Add _
Anchor:=Range(Cells(s, 4), Cells(s, 4)), _
Address:=hyperAddres, _
TextToDisplay:=hyperTxt 'Hyperlink
s = s + 1
Next j
Doc.Close
Next i
MsgBox "Dishes ready Sir!"
End Sub
答案 2 :(得分:0)
我将使用以下更强大的CSS选择器方法来获取类中的所有href
Option Explicit
Public Sub GetLinks()
Dim ie As New InternetExplorer, i As Long, aNodeList As Object
With ie
.Visible = True
.navigate "https://www.oneoiljobsearch.com/senior-reservoir-engineer-jobs/?page=1"
While .Busy Or .readyState < 4: DoEvents: Wend
Set aNodeList = .document.querySelectorAll(".front_job_details [href]")
For i = 0 To aNodeList.Length - 1
Debug.Print aNodeList.item(i)
Next
Stop '<=delete me after
'other stuff
.Quit
End With
End Sub