html - 从 Yahoo Finance (VBA、Excel) 中抓取特定的 <p> 类

标签 html excel vba web-scraping

我一直在尝试从以下位置的代码中的某个嵌套类中提取特定信息
https://finance.yahoo.com/quote/ASUR?p=ASUR

内部文本中所在的类名为“D(ib) Va(t)”,据我所知,至少该文本对于类名是唯一的。我正在使用以下代码来获取数据。

Private Sub CommandButton1_Click()

Dim IE4 As Object
Dim strURL3 As String
Dim divs1 As Object
Dim symbol1 As String
Dim rowd As Integer
Dim divs2 As Object

'turn calculation off

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual

rowd = 1
'Sheets.Add After:=Sheets(ActiveWorkbook.Sheets.Count)
'Sheets(ActiveSheet.Name).Name = "Industry"

'Worksheets("Panel").Activate
'Range("B9").Select
Set IE4 = CreateObject("InternetExplorer.Application") 'Create only one IE instance


'Do Until ActiveCell.Value = "" 'Loop
'symbol1 = ActiveCell.Value

strURL3 = "https://finance.yahoo.com/quote/ASUR?p=ASUR"

IE4.Visible = True 'Flag to remove IE visibility
        VBA.Shell "RunDll32.exe InetCpl.Cpl, ClearMyTracksByProcess 264", vbMinimizedNoFocus
        VBA.Shell "RunDll32.exe InetCpl.Cpl, ClearMyTracksByProcess 258", vbMinimizedNoFocus
        IE4.Navigate strURL3
        While IE4.Busy: DoEvents: Wend 'Break of 4 seconds after loading
        Application.Wait (Now + TimeValue("0:00:04"))


Set divs1 = IE4.Document.getelementsbytagname("div")

  Worksheets("Industry").Activate
  ActiveSheet.Cells("1,2").Select
 For Each div In divs1
          Set divs2 = IE4.Document.getelementsbytagname("p")
           For Each p In divs2

           If p.classname = "D(ib) Va(t)" Then

            Debug.Print p.innertext

            'Cells(rowd, 2) = p.innertext
            'rowd = rowd + 1
            End If
          Next p

Next div

 'Sheets("Panel").Select
' ActiveCell.Offset(1, 0).Select
 'Loop
    'Sheets("Panel").Select
    'Range("B9").Select 'range that selects rows and columns to paste in every company sheet
    'Range(Selection, Selection.End(xlDown)).Select
    'Range(Selection, Selection.End(xlDown)).Select
    'Range(Selection, Selection.End(xlDown)).Select
   ' Selection.Copy
   ' Sheets("Industry").Select
    'Range("A1").Select
    'ActiveSheet.Paste
   ' Application.CutCopyMode = False

 IE4.Quit

'turn calculation on
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

它没有捕获类(class),任何人都可以帮助我吗?

最佳答案

只要有办法避免这种情况,选择复合类就不是一个好习惯。查看以下实现以获得相同的结果:

Sub FetchText()
    Const Link$ = "https://finance.yahoo.com/quote/ASUR?p=ASUR"
    Dim Http As New XMLHTTP60, Htmldoc As New HTMLDocument

    With Http
        .Open "GET", Link, False
        .send
        Htmldoc.body.innerHTML = .responseText
    End With
    MsgBox Htmldoc.querySelector("p.businessSummary").PreviousSibling.LastChild.innerText
End Sub

关于html - 从 Yahoo Finance (VBA、Excel) 中抓取特定的 <p> 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54453890/

相关文章:

javascript - 在angularjs中添加背景图片,css不起作用

html - 将列表保持在同一行

python - "sort by x (ascending) then by y (descending)"表示 python 中的 csv 文件

excel - VBA 用户窗体问题 - 无法设置列表属性。无效的属性数组索引

vba - "<Application-defined or object-defined error> Error 1004"

JQuery UI 菜单滚动

php - 如何防止 WordPress 在 WYSIWYG 编辑器中向元素添加额外的类

vba - 一次删除多张纸

VBA 使用列表过滤和复制行

excel - 子函数显示用户窗体