网页分页没有改变
Web page pagination not changing
我正在尝试导航到网站的下一页,通常这对我有用。但是我现在正在挣扎。目前使用这行代码 Set nextPageElement = HTML.getElementsByClassName("paginationMini--right__active")(0)
我可以循环 X 次,但它不会更改页面,该页面始终保持在第 1 页,因此如果我声明 3 页,它将从第 1 页提取相同的数据三次。什么时候应该换页3次。
我尝试了几种变体,并在下面的代码中留下了一些注释。所有尝试都在第一页之后结束,上面的代码行是唯一将代码循环 3 次但未更改页面的代码行。我一直使用此代码,所以我知道它有效。请有人指出正确的 class.
'Searches Number of Pages entered in Sheet20
If pageNumber >= Replace(Worksheets("Sheet20").Range("J9").Value, "", "+") Then Exit Do
On Error Resume Next
Set nextPageElement = HTML.getElementsByClassName("paginationMini--right__active")(0) ' THIS LINE
'Set nextPage = HTML.getElementsByClassName("pagination--ul")(0).getElementsByClassName("pagination--li")(0).getElementsByTagName("a")(0)
'Set nextPage = HTML.querySelector(".pagination--ul > li.pagination--li > a")
'Set nextPage = HTML.getElementsByClassName("pagination--ul")(0).getElementsByClassName("pagination--li")(0).getElementsByClassName("paginationMini--right__active")(0)
'Set nextPageElement = HTML.getElementsByClassName("paginationMini--ul")(0).getElementsByTagName("li")(2).getElementsByTagName("a")(0)
'Set nextPageElement = HTML.getElementsByClassName("paginationMini")(0).getElementsByTagName("li")(2).getElementsByTagName("a")(0)
If nextPageElement Is Nothing Then Exit Do
nextPageElement.Click 'next web page
Do While objIE.Busy = True Or objIE.readyState <> 4
Loop
Set Html = objIE.document
pageNumber = pageNumber + 1
<div class="search-page__pagination-wrapper">
<nav class="pagination">
<ul class="pagination--ul">
<li class="pagination--li">
<span class="pagination--left__inactive">
<i class="icon">
<svg><use xlink:href="/templates/_generated/svg_icons/common.svg#icon-arrow-left"></use></svg>
</i>
<span class="pagination__text">Previous</span>
</span>
</li>
<li class="pagination--li">
1
</li>
<li class="pagination--li">
<a href="https://www.autotrader.co.uk:443/car-search/page/2" rel="nofollow" data-paginate="2" data-to-top="true">2</a>
</li>
<li class="pagination--li">
<a href="https://www.autotrader.co.uk:443/car-search/page/3" rel="nofollow" data-paginate="3" data-to-top="true">3</a>
</li>
<li class="pagination--li">
<a href="https://www.autotrader.co.uk:443/car-search/page/4" rel="nofollow" data-paginate="4" data-to-top="true">4</a>
</li>
<li class="pagination--li">
<a href="https://www.autotrader.co.uk:443/car-search/page/5" rel="nofollow" data-paginate="5" data-to-top="true">5</a>
</li>
<li class="pagination--li">
<a href="https://www.autotrader.co.uk:443/car-search/page/6" rel="nofollow" data-paginate="6" data-to-top="true">6</a>
</li>
<li class="pagination--li">
<a class="pagination--right__active" href="https://www.autotrader.co.uk:443/car-search/page/2" rel="nofollow" data-paginate="2" data-to-top="true">
<i class="icon">
<svg><use xlink:href="/templates/_generated/svg_icons/common.svg#icon-arrow-right"></use></svg>
</i>
<span class="pagination__text">Next</span>
</a>
</li>
</ul>
</nav>
</div>
尝试这种方式从下一页抓取内容。连接到下一页的链接是无效的。当您单击下一页链接时,它们会被重定向到其他 url。但是,以下是完成任务的简单方法之一:
Sub FetchNextPageContent()
Dim IE As Object, post As Object, Url$, I&
Set IE = CreateObject("InternetExplorer.Application")
Url = "https://www.autotrader.co.uk/car-search?sort=relevance&postcode=W1K%203RA&radius=1500&include-delivery-option=on&page="
For I = 1 To 5
IE.Visible = True
IE.navigate Url & I
While IE.Busy = True Or IE.readyState < 4: DoEvents: Wend
For Each post In IE.document.getElementsByClassName("search-page__result")
With post.getElementsByClassName("listing-fpa-link")
If .Length Then Debug.Print .Item(0).getAttribute("href")
End With
Next post
Next I
End Sub
如果您想坚持单击下一页按钮,则应执行以下操作:
Sub FetchNextPageContent()
Dim IE As Object, post As Object, Url$, I&, nextPage As Object
Dim Html As HTMLDocument
Set IE = CreateObject("InternetExplorer.Application")
Url = "https://www.autotrader.co.uk/car-search?sort=relevance&postcode=W1K%203RA&radius=1500&include-delivery-option=on&page=1"
IE.Visible = True
IE.navigate Url
Do
While IE.Busy = True Or IE.readyState < 4: DoEvents: Wend
Set Html = IE.document
For Each post In Html.getElementsByClassName("search-page__result")
With post.getElementsByClassName("listing-fpa-link")
If .Length Then Debug.Print .Item(0).getAttribute("href")
End With
Next post
Set nextPage = Html.querySelector("a.pagination--right__active")
If Not nextPage Is Nothing Then
nextPage.Click
Application.Wait Now + TimeValue("00:00:05")
Else:
Exit Do
End If
Loop
End Sub
我正在尝试导航到网站的下一页,通常这对我有用。但是我现在正在挣扎。目前使用这行代码 Set nextPageElement = HTML.getElementsByClassName("paginationMini--right__active")(0)
我可以循环 X 次,但它不会更改页面,该页面始终保持在第 1 页,因此如果我声明 3 页,它将从第 1 页提取相同的数据三次。什么时候应该换页3次。
我尝试了几种变体,并在下面的代码中留下了一些注释。所有尝试都在第一页之后结束,上面的代码行是唯一将代码循环 3 次但未更改页面的代码行。我一直使用此代码,所以我知道它有效。请有人指出正确的 class.
'Searches Number of Pages entered in Sheet20
If pageNumber >= Replace(Worksheets("Sheet20").Range("J9").Value, "", "+") Then Exit Do
On Error Resume Next
Set nextPageElement = HTML.getElementsByClassName("paginationMini--right__active")(0) ' THIS LINE
'Set nextPage = HTML.getElementsByClassName("pagination--ul")(0).getElementsByClassName("pagination--li")(0).getElementsByTagName("a")(0)
'Set nextPage = HTML.querySelector(".pagination--ul > li.pagination--li > a")
'Set nextPage = HTML.getElementsByClassName("pagination--ul")(0).getElementsByClassName("pagination--li")(0).getElementsByClassName("paginationMini--right__active")(0)
'Set nextPageElement = HTML.getElementsByClassName("paginationMini--ul")(0).getElementsByTagName("li")(2).getElementsByTagName("a")(0)
'Set nextPageElement = HTML.getElementsByClassName("paginationMini")(0).getElementsByTagName("li")(2).getElementsByTagName("a")(0)
If nextPageElement Is Nothing Then Exit Do
nextPageElement.Click 'next web page
Do While objIE.Busy = True Or objIE.readyState <> 4
Loop
Set Html = objIE.document
pageNumber = pageNumber + 1
<div class="search-page__pagination-wrapper">
<nav class="pagination">
<ul class="pagination--ul">
<li class="pagination--li">
<span class="pagination--left__inactive">
<i class="icon">
<svg><use xlink:href="/templates/_generated/svg_icons/common.svg#icon-arrow-left"></use></svg>
</i>
<span class="pagination__text">Previous</span>
</span>
</li>
<li class="pagination--li">
1
</li>
<li class="pagination--li">
<a href="https://www.autotrader.co.uk:443/car-search/page/2" rel="nofollow" data-paginate="2" data-to-top="true">2</a>
</li>
<li class="pagination--li">
<a href="https://www.autotrader.co.uk:443/car-search/page/3" rel="nofollow" data-paginate="3" data-to-top="true">3</a>
</li>
<li class="pagination--li">
<a href="https://www.autotrader.co.uk:443/car-search/page/4" rel="nofollow" data-paginate="4" data-to-top="true">4</a>
</li>
<li class="pagination--li">
<a href="https://www.autotrader.co.uk:443/car-search/page/5" rel="nofollow" data-paginate="5" data-to-top="true">5</a>
</li>
<li class="pagination--li">
<a href="https://www.autotrader.co.uk:443/car-search/page/6" rel="nofollow" data-paginate="6" data-to-top="true">6</a>
</li>
<li class="pagination--li">
<a class="pagination--right__active" href="https://www.autotrader.co.uk:443/car-search/page/2" rel="nofollow" data-paginate="2" data-to-top="true">
<i class="icon">
<svg><use xlink:href="/templates/_generated/svg_icons/common.svg#icon-arrow-right"></use></svg>
</i>
<span class="pagination__text">Next</span>
</a>
</li>
</ul>
</nav>
</div>
尝试这种方式从下一页抓取内容。连接到下一页的链接是无效的。当您单击下一页链接时,它们会被重定向到其他 url。但是,以下是完成任务的简单方法之一:
Sub FetchNextPageContent()
Dim IE As Object, post As Object, Url$, I&
Set IE = CreateObject("InternetExplorer.Application")
Url = "https://www.autotrader.co.uk/car-search?sort=relevance&postcode=W1K%203RA&radius=1500&include-delivery-option=on&page="
For I = 1 To 5
IE.Visible = True
IE.navigate Url & I
While IE.Busy = True Or IE.readyState < 4: DoEvents: Wend
For Each post In IE.document.getElementsByClassName("search-page__result")
With post.getElementsByClassName("listing-fpa-link")
If .Length Then Debug.Print .Item(0).getAttribute("href")
End With
Next post
Next I
End Sub
如果您想坚持单击下一页按钮,则应执行以下操作:
Sub FetchNextPageContent()
Dim IE As Object, post As Object, Url$, I&, nextPage As Object
Dim Html As HTMLDocument
Set IE = CreateObject("InternetExplorer.Application")
Url = "https://www.autotrader.co.uk/car-search?sort=relevance&postcode=W1K%203RA&radius=1500&include-delivery-option=on&page=1"
IE.Visible = True
IE.navigate Url
Do
While IE.Busy = True Or IE.readyState < 4: DoEvents: Wend
Set Html = IE.document
For Each post In Html.getElementsByClassName("search-page__result")
With post.getElementsByClassName("listing-fpa-link")
If .Length Then Debug.Print .Item(0).getAttribute("href")
End With
Next post
Set nextPage = Html.querySelector("a.pagination--right__active")
If Not nextPage Is Nothing Then
nextPage.Click
Application.Wait Now + TimeValue("00:00:05")
Else:
Exit Do
End If
Loop
End Sub