Html 使用单击按钮进行网页抓取

Html 使用单击按钮进行网页抓取,html,excel,vba,web-scraping,Html,Excel,Vba,Web Scraping,团队,我正在尝试单击“加载更多”按钮,我可以单击并运行宏,而只需单击一次就不会出现任何问题。这是第一次。 我需要以下几点的帮助 我正在尝试自动执行代码,以重复单击按钮,直到页面加载所有用于web抓取的数据 此外,我需要一个代码来检查网页中的加载更多按钮是否存在,然后再将数据发送到excel。如果未找到“加载更多”按钮,请继续执行下一个代码。仅供参考,我的网页底部有更多内容 谢谢,如果我的问题不清楚,请回答我 下面是单击“加载更多”按钮之前的Html代码 你可以试试这个。如果URL不起作用,你可以

团队,我正在尝试单击“加载更多”按钮,我可以单击并运行宏,而只需单击一次就不会出现任何问题。这是第一次。 我需要以下几点的帮助

我正在尝试自动执行代码,以重复单击按钮,直到页面加载所有用于web抓取的数据

此外,我需要一个代码来检查网页中的加载更多按钮是否存在,然后再将数据发送到excel。如果未找到“加载更多”按钮,请继续执行下一个代码。仅供参考,我的网页底部有更多内容

谢谢,如果我的问题不清楚,请回答我

下面是单击“加载更多”按钮之前的Html代码


你可以试试这个。如果URL不起作用,你可以发布它吗

Sub Abc()

Dim browser As Object
Dim url As String
Dim nodeButton As Object
Dim noButtonFound As Boolean

  url = "Your URL here"

  'Initialize Internet Explorer, set visibility,
  'call URL and wait until page is fully loaded
  Set browser = CreateObject("internetexplorer.application")
  browser.Visible = False
  browser.navigate url
  Do Until browser.ReadyState = 4: DoEvents: Loop

  'Click button as often as found
  Do
    'Try to catch button
    Set nodeButton = browser.document.getElementsByTagName("button")(0)

    'Check if button was found
    If Not nodeButton Is Nothing Then
      'Check if it has an style attribute
      If nodeButton.hasAttribute("style") Then
        'Check if button is visible
        If nodeButton.getAttribute("style") <> "display: none;" Then
          'Click button
          nodeButton.Click

          'Wait for load more data
          Application.Wait (Now + TimeSerial(0, 0, 5))
        End If
          'No visible button found, leave loop
          noButtonFound = True
        End If
      Else
        'No visible button found, leave loop
        noButtonFound = True
      End If
    Else
      'No visible button found, leave loop
      noButtonFound = True
    End If
  Loop Until noButtonFound

  'All dynamic data was load
  'Do here what ever you want
  'But I think you don't need a new html document
End Sub

两个问题:1。当你点击按钮时,你是否检查了页面是否在IE中工作?我最近想自动加载更多按钮,但为按钮存储的页面脚本在IE.2中不起作用。按钮是页面上的唯一按钮还是HTML源代码中的第一个按钮?您使用querySelector,它精确地定位找到的第一个按钮。@Zwenn,1。是的,目前我正在使用IE,当我点击按钮时,它工作正常。。。2.这是页面上唯一的按钮。当特定页面中的数据较多时,此按钮可用。在某些情况下,您将找不到“加载更多”按钮,因为它的数据有限,完全适合页面…Zwenn,感谢您的回复。。代码工作正常,但在未找到加载按钮之前不会循环。对不起,我不能提供网址。我提到了在单击LoadMore按钮直到它加载web页面中的完整数据之后的html代码。我认为这将有助于完成代码;在多次单击按钮以加载完整页面后添加。@RAJSHA如果我理解正确,按钮始终在那里。它仅在加载所有数据后才隐藏。因此,循环永远不会离开。我在回答中扩展了循环。现在它查询style属性及其值。Zwenn,如果nodeButton.getAttributestyle显示:none;然后是nodeButton。单击未执行。它被跳过了,if条件变成无限循环。@RAJSHA我看到了它,几分钟前又扩展了它。
<button class="btn primary btn-primary modal-button-print add-notes" style="display: none;" type="button" data-bind="click: getWoNotes, visible: isLoadMoreNotesButtonEnable() &amp;&amp; !$root.providerShouldAcceptDecline()">
                <i class="fa fa-refresh" aria-hidden="true"></i>Load More Work Order Notes
            </button>
Sub abc()


Set IE = New InternetExplorer
Link = my url
.
.
.
.
For L = 2 To Lr1
    IE.navigate Link 
    Set Html = New MSHTML.HTMLDocument
    Set Ws = Scraping
    Do
    DoEvents: Loop Until IE.readyState = READYSTATE_COMPLETE
    Application.Wait (Now + TimeValue("00:00:05"))
    IE.document.querySelector("button[type=button]").Click
    Do
    DoEvents: Loop Until IE.readyState = READYSTATE_COMPLETE
    Application.Wait (Now + TimeValue("00:00:05"))
    IE.document.querySelector("button[type=button]").Click
    Do
    DoEvents: Loop Until IE.readyState = READYSTATE_COMPLETE
    Application.Wait (Now + TimeValue("00:00:05"))
    IE.document.querySelector("button[type=button]").Click
    Do
    DoEvents: Loop Until IE.readyState = READYSTATE_COMPLETE
    Application.Wait (Now + TimeValue("00:00:05"))

    Html.body.innerHTML = IE.document.querySelectorAll(".list").Item(1).outerHTML
    Set Tariku = Html.querySelectorAll(".columns")
    Set data = Html.querySelectorAll(".datalist")
        With Ws

        ' Do all the stuff  

        End With
        IE.document.querySelector("#Logout").Click
        IE.Quit
       Exit Sub

  Next L

End Sub
Sub Abc()

Dim browser As Object
Dim url As String
Dim nodeButton As Object
Dim noButtonFound As Boolean

  url = "Your URL here"

  'Initialize Internet Explorer, set visibility,
  'call URL and wait until page is fully loaded
  Set browser = CreateObject("internetexplorer.application")
  browser.Visible = False
  browser.navigate url
  Do Until browser.ReadyState = 4: DoEvents: Loop

  'Click button as often as found
  Do
    'Try to catch button
    Set nodeButton = browser.document.getElementsByTagName("button")(0)

    'Check if button was found
    If Not nodeButton Is Nothing Then
      'Check if it has an style attribute
      If nodeButton.hasAttribute("style") Then
        'Check if button is visible
        If nodeButton.getAttribute("style") <> "display: none;" Then
          'Click button
          nodeButton.Click

          'Wait for load more data
          Application.Wait (Now + TimeSerial(0, 0, 5))
        End If
          'No visible button found, leave loop
          noButtonFound = True
        End If
      Else
        'No visible button found, leave loop
        noButtonFound = True
      End If
    Else
      'No visible button found, leave loop
      noButtonFound = True
    End If
  Loop Until noButtonFound

  'All dynamic data was load
  'Do here what ever you want
  'But I think you don't need a new html document
End Sub