【发布时间】:2021-09-20 23:07:02
【问题描述】:
我有一个包含 1000 个页面的网站,我想从单个工作表中的每个页面中提取表格。我尝试过的是将页数放在循环中并提取数据。这样做的问题是它不是自动化的,它会在新工作表中提取每个页面的表格数据。
有效的解决方案可能是什么,这样可以在单张纸上完成网站每个页面的表格提取,因为有数千个页。 所以我的问题是:
1.如何从网站获取页面数,以便我可以循环使用它,即For i = 1 To Numberofpages?或者除了获取页数之外还有其他方法(例如Going to next page until next page does not appearing)。
2.如何将每页提取的所有表格放在一个工作表上?
Sub Scrape()
Dim ResultSections As Selenium.WebElements
Dim ResultSection As Selenium.WebElement
Dim i As Long
Set ch = New Selenium.ChromeDriver
ch.Start baseUrl:="http://www.nepalstock.com"
For i = 1 To 3 '3 is number of pages TO BE FOUND which here is taken for Example
ch.Get "/main/floorsheet/index/" & i & "/?contract-no=&stock-symbol=&buyer=&seller=&_limit=30"
Set ResultSections = ch.FindElementsByClass("my-table")
For Each ResultSection In ResultSections
ResultSection.AsTable.ToExcel ThisWorkbook.Worksheets.Add.Range("A1")
Next ResultSection
Next
End Sub
【问题讨论】:
标签: vba selenium google-chrome selenium-chromedriver