Extracting table data from website using chrome

笑着哭i 提交于 2021-02-11 15:09:40

问题


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

回答1:


The following script should fetch you the tabular content from that page.

Sub GetTabularcontent()
    Const Url$ = "https://chartink.com/screener/buy-15m-78"
    Dim driver As New ChromeDriver, tRow As Object
    Dim tCel As Object, R&, C&

    With driver
        .get Url

        For Each tRow In .FindElementsByCss("#DataTables_Table_0 tr", Timeout:=10000)
            For Each tCel In tRow.FindElementsByCss("th,td")
                C = C + 1: Cells(R + 1, C) = tCel.Text
            Next tCel
            C = 0: R = R + 1
        Next tRow
    End With
End Sub



回答2:


@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


来源:https://stackoverflow.com/questions/60484591/extracting-table-data-from-website-using-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!