Excel VBA从网站表中提取数据

时间:2018-10-22 20:34:03

标签: html excel vba internet-explorer web-scraping

我正在尝试抓取网站,仅需要1个值。 如何使用以下代码检索购买方式?参见下面的html

Private Sub CommandButton1_Click()
    Dim IE As Object
    ' Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")
    ' You can uncoment Next line To see form results
    IE.Visible = False
    ' URL to get data from
    IE.Navigate Cells(1, 1)
    ' Statusbar
    Application.StatusBar = "Loading, Please wait..."
    ' Wait while IE loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 5, Now)
    Loop
    Application.StatusBar = "Searching for value. Please wait..."
    Dim tr As Object, td As Object, tb As Object
    Dim value As String

    Set tb = IE.Document.getElementById("prop_desc clearfix")

    For Each tr In tb.Rows 'loop through the <tr> rows of your table
      For Each td In tr.Cells 'loop through the <th> cells of your row
        value = td.outerText 'your value is now in the variable "value"
        MsgBox value
      Next td
    Next tr

    ' Show IE
    IE.Visible = True
    ' Clean up
    Set IE = Nothing
    Application.StatusBar = ""
End Sub

    </div>
  </div>
  <div class="prop_desc clearfix"><div class = "span-half">
  <h3>RV Park/Campground for Sale</h3>
  <table>

    

  <tr>
    <td>Number of RV Lots: </td>
    <td>270</td>
  </tr>


  <tr>
    <td>Size:</td>
    <td>
      157 acre(s)
    </td>
  </tr>




    <tr>
      <td>Purchase Method:</td>
      <td>Cash, New Loan</td>
    </tr>

  <tr>
    <td>Status:</td>
    <td>
      Active
    </td>
  </tr>


  <tr>
    <td>Property ID:</td>
    <td>966994</td>
  </tr>


    <tr>
      <td>Posted on:</td>
      <td>Jul 10, 2018</td>
    </tr>

    <tr>
      <td>Updated on:</td>
      <td>Jul 10, 2018</td>
    </tr>
    <tr>

2 个答案:

答案 0 :(得分:1)

您可以按索引按类别定位,然后按索引按td定位

Option Explicit

Public Sub GetInfo()
    Dim IE As New InternetExplorer
    With IE
        .Visible = True
        .navigate "https://www.rvparkstore.com/rv-parks/902077--2843-lake-frontage-42-acres-for-sale-in-north-central-us"

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

        Debug.Print .document.getElementsByClassName("span-half")(0).getElementsByTagName("td")(5).innerText

        .Quit '<== Remember to quit application
    End With
End Sub

答案 1 :(得分:0)

更新:

网址https://www.rvparkstore.com/rv-parks/902077--2843-lake-frontage-42-acres-for-sale-in-north-central-us

HTML在https://docs.google.com/document/d/1J5tDV99IbzucCB_z8QX8lDa4X3ecxQbQOWeVy5B7Irg/edit?usp=sharing

请使用链接获取更多信息jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj