Visual Basic - 简单的Web Scraper

时间:2017-02-26 18:39:35

标签: excel excel-vba vba

新手提问;我正在YouTube上关注如何从网页上屏蔽废品信息的网络系列。目标是能够在我所拥有的内部公司应用程序上使用它,它只显示带有文本值的空白网页。然而,两天后我仍然无法弄清楚为什么我不能在VB中进行简单的函数调用工作。到目前为止,我所做的一切都在我的表单上单击按钮“Private Sub CommandButton1_Click()'”时继续抛出编译错误。

以下是按钮的代码:

Private Sub CommandButton1_Click()
'FirstMacro

    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.Top = 0
    objIE.Left = 0
    objIE.Width = 800
    objIE.Height = 600
    objIE.AddressBar = 0
    objIE.StatusBar = 0
    objIE.Toolbar = 0
    objIE.Visible = True 'We will see the window navigation'

    objIE.Navigate ("http://www.google.com")
    TextBox4.Text objIE.Document.body.innerHTML 
End Sub

以下是该类的代码:

Public Function FirstMacro()
'the_start:

    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.Top = 0
    objIE.Left = 0
    objIE.Width = 800
    objIE.Height = 600
    objIE.AddressBar = 0
    objIE.StatusBar = 0
    objIE.Toolbar = 0
    objIE.Visible = True 'We will see the window navigation'

    'MsgBox.Err.Number

    'On Error Resume Next
       'MsgBox.objIE.Document.body.innerHTML
        'If Err.Number > 0 Then
            'objIE.Quit
            'Set objIE = Nothing
            'GoTo the_start:
        'End If

    objIE.Navigate ("http://www.google.com")

    'Do
        'DoEvents
    'Loop Until objIE.ReadyState = 4

    TextBox4.Text objIE.Document.body.innerHTML
End Function

这是我关注的系列:https://www.youtube.com/watch?v=Blls6GrCBCY&index=12&list=PL6OYc4rwKjcOu3UL7LYpvO_S2waYO-hVU

感谢您帮助那个菜鸟。

enter image description here

1 个答案:

答案 0 :(得分:1)

你应该尝试这样的事情。

Sub DumpData()

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

URL = "http://finance.yahoo.com/q?s=sbux&ql=1"

'Wait for site to fully load
IE.Navigate2 URL
Do While IE.Busy = True
   DoEvents
Loop

RowCount = 1

With Sheets("Sheet1")
   .Cells.ClearContents
   RowCount = 1
   For Each itm In IE.document.all
      .Range("A" & RowCount) = itm.tagname
      .Range("B" & RowCount) = itm.ID
      .Range("C" & RowCount) = itm.classname
      .Range("D" & RowCount) = Left(itm.innertext, 1024)

      RowCount = RowCount + 1
   Next itm
End With
End Sub

然后,您应该更好地了解您在每个网址处理的内容。