在表II中抓取特定数据 [英] Scraping specific data inside a table II

查看:69
本文介绍了在表II中抓取特定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我讨厌不得不再次问这个问题,但是我一直在从更新的网站上抓取数据的网站,不仅是从美学上讲,其基础代码也已更改.在更新之前,程序将找到"Key Data".表并使用计数器来查找特定数据.问题是我不再使用这些值,并且当我尝试使用更接近该值的Class Name时,它根本找不到它并退出程序.

I hate that I have to ask this question again but the website I had been scraping data from updated, not just aesthetically, the underlying code has changed too. Before the update, the program would find the "Key Data" table and use a counter to find specific data. The problem is I'm not getting into the values anymore and when I try to use a Class Name closer to the value, it doesn't find it at all and drops out of the program.

我已经裁剪了下面的一些代码以供分享,希望能对您有所帮助.与上次类似,我感觉很近,但很快就会消失.

I've cut out some of the code below to share, would appreciate any help. Similar to last time, I feel so close but am coming up short.

Set ieObj = New InternetExplorer
ieObj.Visible = True
ieObj.navigate "https://web.tmxmoney.com/quote.php?qm_symbol=" & Cells(c, 2) & ":US"

Do While ieObj.readyState <> 4
    Application.StatusBar = "Getting to 'Key Data' Table"
    DoEvents
Loop

d = 0
For Each htmlELE In ieObj.document.getElementsByClassName("sc-kfYqjs jpcbJh")
    
    'scrape EPS amount
    If d = 9 Then
        EPS = htmlELE.innerText
        Range("H2").value = EPS
    End If

    'scrape dividend
    If d = 14 Then
        div = htmlELE.innerText
        Range("I2").value = div
    End If

d = d + 1
Next


推荐答案

这是一个动态页面.向下滚动将加载内容.因此,您必须等待加载第一部分".页面的.然后向下滚动到所需的表1500.向下滚动后,等待加载表.

It's a dynamic page. The content will be load by scrolling down. So you must wait for load the "first part" of the page. Then scroll down to the needed table by 1500. After scrolling down wait to load the table.

您不需要循环就可以抓取所需的值.您可以使用 querySelector()通过其属性名称和属性值获取特定元素.

You don't need a loop to scrape the wanted values. You can use querySelector() to get the specific element by it's attribute name and atttribute value.

在此处查找有关 querySelector()的信息>
以下是示例,您也可以将其与 querySelector()一起使用: querySelectorAll()

Look here for information about querySelector()
And here for examples you can use also with querySelector(): querySelectorAll()

此代码对我有用.如果不是这样,那么您可能需要休息一下,或者可能需要滚动一下:

This code works for me. If not for you play with the breaks and perhaps the amount of scrolling:

Sub test()

Dim ieObj As Object
Dim nodeEps As Object
Dim nodeDividend As Object
Dim eps As String
Dim dividend As String

  Set ieObj = CreateObject("InternetExplorer.Application")
  ieObj.Visible = True
  'ieObj.navigate "https://web.tmxmoney.com/quote.php?qm_symbol=" & Cells(c, 2) & ":US"
  ieObj.navigate "https://web.tmxmoney.com/quote.php?qm_symbol=GE:US"
  
  Do While ieObj.readyState <> 4
    Application.StatusBar = "Getting to 'Key Data' Table"
    DoEvents
  Loop
  Application.Wait (Now + TimeSerial(0, 0, 3))
  ieObj.document.parentWindow.Scroll 0, 1500
  Application.Wait (Now + TimeSerial(0, 0, 2))
  
  'scrape EPS amount
  Set nodeEps = ieObj.document.querySelector("div[data-testid='eps-value']")
  eps = Trim(nodeEps.innerText)
  'Range("H2").Value = eps
  
  'scrape dividend
  Set nodeDividend = ieObj.document.querySelector("div[data-testid='dividendAmount-value']")
  dividend = Trim(nodeDividend.innerText)
  'Range("I2").Value = dividend
  
  'Clean up
  ieObj.Quit
  Set ieObj = Nothing
  Set nodeEps = Nothing
  Set nodeDividend = Nothing
  Application.StatusBar = False
  
  MsgBox eps & Chr(13) & dividend
End Sub

这篇关于在表II中抓取特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆