使用vba从受密码保护的网站刮取数据未定义用户定义的类型

时间:2018-03-27 10:38:43

标签: excel vba excel-vba web-scraping

我想从受密码保护的网站(https://www.vesseltracker.com/fr/Ports/Home.html)导入数据,我确实有用户名和密码。

我已经尝试过Dick在这个网站上制作的这个VBA代码:http://dailydoseofexcel.com/archives/2006/11/29/html-tables/但它不起作用并且每次我调整它时都会卡住。

它被困在这里:Compile error:  User-defined type not defined

Sub GetTable()

Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim ieTable As Object
Dim clip As DataObject

'create a new instance of ie
Set ieApp = New InternetExplorer

'you don’t need this, but it’s good for debugging
ieApp.Visible = True

'assume we’re not logged in and just go directly to the login page
ieApp.Navigate "https://www.vesseltracker.com/fr/Home.html"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

Set ieDoc = ieApp.Document

'fill in the login form – View Source from your browser to get the control names
With ieDoc.forms(0)
.UserName.Value = "username"
.Password.Value = "password"
.submit
End With
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

'now that we’re in, go to the page we want
ieApp.Navigate "https://www.vesseltracker.com/fr/Port/tangermed/Dashboard.html"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

'get the table based on the table’s id
Set ieDoc = ieApp.Document
Set ieTable = ieDoc.all.Item

'copy the tables html to the clipboard and paste to teh sheet
If Not ieTable Is Nothing Then
Set clip = New DataObject
clip.SetText "" & ieTable.outerHTML & ""
clip.PutInClipboard
Sheet1.Select
Sheet1.Range("A1").Select
Sheet1.PasteSpecial "Unicode Text"
End If

'close 'er up
ieApp.Quit
Set ieApp = Nothing

End Sub

我真的很感谢你的任何帮助。谢谢。

2 个答案:

答案 0 :(得分:2)

类型DataObject未定义,因为引用中不存在MSForms。

您可以使用后期绑定来设置剪贴板中的某些文本:

With VBA.CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
  .SetText "1234"
  .PutInClipboard
End With

答案 1 :(得分:1)

您可以像这样添加所需的引用:

  

您需要包含 MSForms 库。

     

C:\Windows\system32\FM20.DLL

     

最简单的方法是在项目中添加用户表单。   <子>(Source

或者您可以通过以下方式添加库:

工具&gt;参考文献&gt;浏览&gt; C:\Windows\system32\FM20.DLL