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

标签 vba excel 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

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

最佳答案

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

您可以改用后期绑定(bind)在剪贴板中设置一些文本:

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

关于vba - 使用 vba 从受密码保护的网站中抓取数据用户定义类型未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49510711/

相关文章:

python - Beautifulsoup:如何在其他元素之间获取元素

css - 网络抓取(抓取)时, "li: nth-child (n)"如何将数字 n 增加 +1?

excel - 如果电子邮件主题行以某些值开头,则执行某些操作

excel - 如何从海量数据中查找等于今天的日子

excel - 列出excel中的重复值

excel - VBA Excel 中非连续范围的最小值

javascript - 如何使用selenium从onclick javascript中提取url : Python

Excel 数据验证以另一个单元格值为条件

excel - 如果目标目录中存在文件,则从当前目录中删除文件的最佳方法

VBA代码设置打印区域,适合1x1页面并且不为某些选项卡设置打印区域