VBA parsing of href

2020-08-05 11:02发布

I've googled and also searched many posts on Stack some of them interesting for this1,2,3,4

I'll show you what i am after. I picked a random site and copied a few lines (from 155 to 174)

<ul>
<li><a href="/wiki/B.A.C._I" title="B.A.C. I" class="mw-redirect">B.A.C. I</a></li>
<li><a href="/wiki/B.A.C._II" title="B.A.C. II" class="mw-redirect">B.A.C. II</a></li>
<li><a href="/wiki/B.A.C._III" title="B.A.C. III" class="mw-redirect">B.A.C. III</a></li>
<li><a href="/wiki/B.A.C._IV" title="B.A.C. IV" class="mw-redirect">B.A.C. IV</a></li>
<li><a href="/wiki/B.A.C._V" title="B.A.C. V" class="mw-redirect">B.A.C. V</a></li>
<li><a href="/wiki/B.A.C._VI" title="B.A.C. VI" class="mw-redirect">B.A.C. VI</a></li>
<li><a href="/wiki/B.A.C._VII" title="B.A.C. VII" class="mw-redirect">B.A.C. VII</a></li>
<li><a href="/wiki/B.A.C._VII_Mk.2" title="B.A.C. VII Mk.2" class="mw-redirect">B.A.C. VII Mk.2</a></li>
<li><a href="/wiki/B.A.C._VII_Planette" title="B.A.C. VII Planette" class="mw-redirect">B.A.C. VII Planette</a></li>
<li><a href="/wiki/B.A.C._VIII" title="B.A.C. VIII" class="mw-redirect">B.A.C. VIII</a></li>
<li><a href="/wiki/B.A.C._VIII_Bat-Boat" title="B.A.C. VIII Bat-Boat" class="mw-redirect">B.A.C. VIII Bat-Boat</a></li>
<li><a href="/wiki/B.A.C._IX" title="B.A.C. IX" class="mw-redirect">B.A.C. IX</a></li>
<li><a href="/wiki/B.A.C._Cupid" title="B.A.C. Cupid" class="mw-redirect">B.A.C. Cupid</a></li>
<li><a href="/wiki/B.A.C._Drone" title="B.A.C. Drone" class="mw-redirect">B.A.C. Drone</a></li>
<li><a href="/wiki/B.A.C._Super_Drone" title="B.A.C. Super Drone" class="mw-redirect">B.A.C. Super Drone</a></li>
<li><a href="/wiki/B.A._Swallow_2" title="B.A. Swallow 2" class="mw-redirect">B.A. Swallow 2</a></li>
<li><a href="/wiki/B.A._Eagle_2" title="B.A. Eagle 2" class="mw-redirect">B.A. Eagle 2</a></li>
<li><a href="/wiki/B.A._Double_Eagle" title="B.A. Double Eagle" class="mw-redirect">B.A. Double Eagle</a></li>
</ul>

I need to find one value between the HTML tags for example B.A.C. VII and then use VBA to actually go in to the link href of the line item which holds B.A.C. VII. After i have navigated there i need to get the resulting URL in a String. Keep in mind that i need highly prefer binding if possible. I tried to write a loop but with no success...

Dear Ron

Debugger stops on the first line andd... It giving me Run-time error '424' however we have an object qualifier everywhere...

enter image description here

thanks for watching my question

标签: vba excel
1条回答
欢心
2楼-- · 2020-08-05 11:25

This loop works for me.

Sub test()
' open IE, navigate to the website of interest and loop until fully loaded
Set IE = CreateObject("InternetExplorer.Application")
my_url = "http://www.xyz.com"

With IE
    .Visible = True
    .navigate my_url
    .Top = 50
    .Left = 530
    .Height = 400
    .Width = 400

Do Until Not IE.Busy And IE.readyState = 4
    DoEvents
Loop

End With

' Find the desired url and click
Set Results = IE.document.getElementsByTagName("a")
For Each itm In Results
    If itm.outerhtml = "B.A.C. VII" Then
        itm.Click

        Do Until Not IE.Busy And IE.readyState = 4
            DoEvents
        Loop
        Exit For
    End If
Next
End Sub
查看更多
登录 后发表回答