Excel VBA-Web报废<a> Href

时间:2018-03-29 10:59:52

标签: excel-vba web-scraping vba excel

I created a macro that Extract Data from web Page and then print it into an excel Sheet . The code works fine for :

Openning Web Page and changing from page into another

but the problems is :

I can't click on Href link to access to that specific Page

the Html for href is this :

<a class=onglet href="/cmh/consultation/preViewMODScheduling.do">Scheduling</a>

and the html container of that <a> is this

 <TD width=200><TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
   <TBODY>
      <TR>
        <TD width=11><IMG src="/cmh/cmh/image/Tab1gh.gif" width=11 
          height=20></TD>
        <TD background=/cmh/cmh/image/Tab1mat.gif align=center><A 
         class=onglet href="/cmh/consultation/preViewMODScheduling.do? 
          fromSelect=true">Scheduling</A></TD>
        <TD width=11><IMG src="/cmh/cmh/image/Tab1dr.gif" width=11 
               height=20>
       </TD>
    </TR>
   </TBODY>
  </TABLE>
</TD>

My code :

Sub extt()

Dim IE As Object, obj As Object
Dim itm As IHTMLElement
Dim r As Long, c As Long, t As Long, d As Long
Dim lastRow As Variant
Dim elemCollection As Object
Dim ele As Object
Dim eRow As Long
Dim f As Variant
Dim oHtml       As HTMLDocument
Dim MPNum As Variant
Dim y As Long, z As Long, wb As Excel.Workbook, ws As Excel.Worksheet
'add the microsoft Internet Controls
Set IE = CreateObject("InternetExplorer.Application")


 With IE

  .Visible = True
  .navigate "http://preSearchMOD.do? 
    clearBackList=true&CMH_NO_STORING_fromMenu=true"

 While IE.Busy Or IE.readyState <> 4: DoEvents: Wend
   'we ensure that the web Page is loaded completely


  Set itm = IE.document.getElementsByName("searchById")(0)
     If Not itm Is Nothing Then itm.Value = Sheets("GDC").Range("C9").Value
  Set doc = IE.document


  Set tags = IE.document.getElementsByTagName("input")

  For Each tagx In tags
     If tagx.src = "htmh/image/button_search.gif" Then
    tagx.Click
   End If
  Next
Set elemCollection = IE.document.getElementsByTagName("a")
Debug.Print elemCollection.Length
'Debug.Print elemCollection.body.innerHTML


For Each tagx In elemCollection
    If tagx.href = "http/consultation/preViewMODScheduling.do? 
  fromSelect=true" Then
    tagx.Click
  Debug.Print tagx.body.innerHTML
End If
Next

myPoints = doc.getElementsByTagName("a")(3)
Range("A1").Value = myPoints

Application.Wait (Now + TimeValue("0:00:03"))
 On Error Resume Next

While IE.Busy Or IE.readyState <> 4: DoEvents: Wend

End With

Set IE = Nothing

MsgBox "Done"
End Sub

this code doesn't return anything and doesn't click on that href

Set elemCollection = IE.document.getElementsByTagName("a")
Debug.Print elemCollection.Length
'Debug.Print elemCollection.body.innerHTML


For Each tagx In elemCollection
    If tagx.href = "http/consultation/preViewMODScheduling.do? 
  fromSelect=true" Then
    tagx.Click
  Debug.Print tagx.body.innerHTML
End If
Next

and also I tried to see how many <a> in that HTML code and I found 76 using : Debug.Print elemCollection.Length

To resume : What I want is to reach that <a> and click on that href to get that Scheduling page

anyone can light me in solving this ?

2 个答案:

答案 0 :(得分:1)

在尝试找到解决方案之后我创建了这个,我知道我的代码是正确的

 Set elements = doc.getElementsByTagName("a")

 For Each element In elements
 If element.getAttribute("class") = "onglet" Then
    If element.href = "Internal 
    PortXXXX/cmh/consultation/preViewMODScheduling.do?fromSelect=true" Then
        element.Click
        Exit For
    End If
End If
Next element

这个解决方案非常适合我

For Each l In Doc.getElementsByTagName("a")
    If l.href = "h/cmh/consultation/preViewMODScheduling.do?fromSelect=true" 
 Then
 l.Click
 end if 
Next

最好的问候

POLOS

答案 1 :(得分:0)

使用CSS选择器

a.onglet[href*="/cmh/consultation/preViewMODScheduling.do?"]

Item selected with CSS query

"."表示课程内容,"*" href包含/cmh....

代码用法:

doc.querySelector("a.onglet[href*=""/cmh/consultation/preViewMODScheduling.do?""]").Click