使用VBA从网页获取数据
我正试图从这个网页获取一些数据:
http://www.vodafone.de/privat/handys-tablets-tarife/smartphone-tarife.html
我希望在每个智能手机和每个合同types的正确站点上有整个表格。
一次性付款设置:Anschlusspreis
一次性付款的手机:智能手机
总金额:Gesamt
合同的月付款:Basispreis
手机每月支付:智能手机Zuzahlung
这全部存储在JavaScript部分,这是一大堆字母。
我正尝试使用Excel VBA:
Sub Button1_Click() 'On Error GoTo Errorhandler Dim ie As Object, doc As Object, rng As Object, ticker As String, quote As String Set ie = CreateObject("InternetExplorer.Application") i = 1 'Application.StatusBar = "Your request is loading. Please wait..." ie.navigate "http://www.vodafone.de/privat/handys-tablets-tarife/smartphone-tarife.html" 'ie.navigate ticker Do DoEvents Loop Until ie.readyState = READYSTATE_COMPLETE Set doc = ie.document quote = doc.getElementsByID("connectionFeeVal").innerText Cells(3, 3).Value = quote MsgBox ("done") 'Errorhandler: 'If Err = 91 Then MsgBox "Error Message" ie.Application.Quit End Sub
但它在“DoEvents”中不断循环。
有人有一个想法,为什么以及如何解决这个问题,也许另一个想法如何从这个页面中获取所有的数据。
先谢谢你。
而不是使用IE自动化,你也可以使用一个http请求对象:
Dim oRequest As Object Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1") oRequest.Open "GET", "http://www.vodafone.de/privat/handys-tablets-tarife/smartphone-tarife.html" oRequest.Send MsgBox oRequest.ResponseText
它的速度更快,不需要像IE解决scheme那样多的资源
如果你在代理服务器的后面,你可以使用这样的东西:
Const HTTPREQUEST_PROXYSETTING_PROXY = 2 Dim oRequest As Object Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1") oRequest.setProxy HTTPREQUEST_PROXYSETTING_PROXY, "http://proxy.intern:8080" oRequest.Open "GET", "http://www.vodafone.de/privat/handys-tablets-tarife/smartphone-tarife.html" oRequest.Send MsgBox oRequest.ResponseText
当然你必须将代理调整到你的值。
正如你在德国的网页这里也有一个简短的德语说明: http : //cboden.de/softwareentwicklung/vba/tipps-tricks/27-webseite-per-vba-ansprechen还有解释如何传递值一个表单的Web服务器,这也可能是对你有帮助。
代替:
Do DoEvents Loop Until ie.readyState = READYSTATE_COMPLETE
你可以尝试:
Do While ie.Busy: DoEvents: Loop Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop with ie ... end with