本文介绍了使用Chrome从网站中提取表格数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有硒元素的chrome浏览器从网站中提取表格数据.我写了下面的代码,但是没有用

i want to extract table data from website with chrome browser with selenium. i wrote below code but it's not working

Sub Chartinka()
    Dim bot As New WebDriver, posts As WebElements, post As WebElement, i As Integer, mysheet As Worksheet, keys As Selenium.keys
    bot.Start "chrome", "https://chartink.com/screener/buy-15m-78"
    bot.Get "/"
    Set posts = bot.FindElementsByXPath("//*[@id='DataTables_Table_0']/tbody/tr[1]")
    i = 2
    Set mysheet = Sheets("Sheet3")
    For Each post In posts
            ' Run time Error '438' on the next line
            mysheet.Cells(i, 1).Value = post.FindElementByTag("td")(0).text
            mysheet.Cells(i, 2).Value = post.FindElementByTag("td")(1).text
            mysheet.Cells(i, 3).Value = post.FindElementByTag("td")(2).text
            mysheet.Cells(i, 4).Value = post.FindElementByTag("td")(3).text
            mysheet.Cells(i, 5).Value = post.FindElementByTag("td")(4).text
            mysheet.Cells(i, 6).Value = "BUY"
        i = i + 1
    Next
  bot.Quit
End Sub

推荐答案

@asmitu您可以使用这样的代码进行无循环操作

@asmitu You can do without loop using such a code

Sub GetTable()
Const sURL As String = "https://chartink.com/screener/buy-15m-78"
Dim bot As New ChromeDriver, tbl As Selenium.TableElement

With bot
    .Get sURL

    Set tbl = .FindElementById("DataTables_Table_0").AsTable
    tbl.ToExcel ThisWorkbook.Sheets("Sheet1").Range("A1")
End With
End Sub

这篇关于使用Chrome从网站中提取表格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:08