如何使用excel vba获取href属性

问题描述 投票:2回答:3

我想检索html页面的<h3>标签的href属性,我能够获取innerText,但我不知道如何访问href属性,文档中有几个<h3>标签,但是时间因为我只需要第一个。我稍后会处理其余的事......

这是我到目前为止的代码

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

以下是从以下位置提取数据的HTML文档的一部分:

  <div class="front_job_details">

    <h3>
        <a href="/jobs/senior-reservoir-engineer-oslo-norway-7?cmp=js&from=job-search-form-2" target="_blank">

        Senior Reservoir Engineer

        </a>
    </h3>

我需要检索的文字是:“/ jobs / senior-reservoir-engineer-oslo-norway-7?cmp = js&from = job-search-form-2”

在此先感谢您的帮助。

excel vba web-scraping href
3个回答
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的第一个<A>标签。您可以复制该方法以从每个H3获得多个A.


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

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
© www.soinside.com 2019 - 2024. All rights reserved.