VBA

2017-09-04 11 views
0

私は研究目的のために、次のページをこすりしようとしてきたとウェブサイトからのdivクラス情報の抽出:VBA

<div class="panel panel-default"> 
    <div class="panel-heading" data-activity="22196 - Manufacturer" data-products='["Products", "Information"]' data-range="Value" data-contact="Person" data-site="www.website.com.br" data-emails="[email protected]" data-phones="Phone" data-address="Street/City" data-countries='["Country1", "Country2"]' data-name="ACME Corp."> 
    <h3 class="panel-title"> 
     <button class="btn btn-link" data-toggle="modal" data-target="#company-modal"> 
     ACME Corp. 
     </button> 
    </h3> 
    <button class="btn btn-primary btn-lg pull-right" data-toggle="modal" data-target="#company-modal"> 
     <i class="icon-plus"></i> 
    </button> 
    </div> 
</div> 
http://www.brazil4export.com/en/pesquisa/resultado/?page=1&

私はから情報を取得したいHTMLの一部は以下のとおりです。

ページ上の各結果については、上記と同様に<div class="panel panel-default">があり、それぞれからdata-namedata-siteという情報を取得したいと考えています。これは私がこれまでのところ、試したものです:

Sub useClassnames() 
    Dim element As IHTMLElement 
    Dim elements As IHTMLElementCollection 
    Dim ie As InternetExplorer 
    Dim html As HTMLDocument 

    'open Internet Explorer in memory, and go to website 
    Set ie = New InternetExplorer 

    ie.Visible = True 
    ie.navigate "http://www.brazil4export.com/en/pesquisa/resultado/?page=1&" 
    'Wait until IE has loaded the web page 

    Do While ie.READYSTATE <> READYSTATE_COMPLETE 
    Application.StatusBar = "Loading Web page …" 
    DoEvents 
    Loop 

    Set html = ie.document 
    Set elements = html.getElementsByClassName("panel panel-default") 

    Dim erow As Long 

    For Each element In elements 
    If element.className = "data-name" Then 
     erow = Sheet1.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row 
     Cells(erow, 2) = html.getElementsByClassName("data-name").innerText 
    End If 

    If element.className = "data-site" Then 
     erow = Sheet1.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row 
     Cells(erow, 3) = html.getElementsByClassName("data-site").innerText 
    End If 
    Next element 

End Sub 

それは動作しませんが、同様に私のすべてのエラーを表示しません。

+0

あなたが「データ名」と呼ばれるクラス名を探していますが、それと呼ばれる何のクラスがありません掲載スニペットにされています。あなたは代わりに "パネル見出し"を見つけようとしていますか? – Moacir

+0

実際には、 'panel panel-default'をすべて調べ、' data-name'と 'data-site'を取得しようとしています – Serveira

+0

' data-name'と 'data-site'は' div'の属性ですクラスではありません。 – UGP

答えて

1

を実行し、この、あなたはすべての結果があります:

Sub WebData() 
    Dim http As New XMLHTTP60, html As New HTMLDocument 
    Dim source As Object 

    With http 
     .Open "GET", "http://www.brazil4export.com/en/pesquisa/resultado/?page=1&", False 
     .send 
     html.body.innerHTML = .responseText 
    End With 
    For Each source In html.getElementsByClassName("panel-heading") 
     x = x + 1: Cells(x, 1) = source.getAttribute("data-Name") 
     Cells(x, 2) = source.getAttribute("data-site") 
    Next source 
End Sub 

は、参照ライブラリへの「Microsoft HTMLオブジェクトライブラリ」と「マイクロソフトのXML」を追加してください。結果の写真を参照してください。

enter image description here

+0

これは完璧に動作します! – Serveira