使用VBA从网页中获取数据

时间:2014-10-28 16:05:36

标签: excel vba excel-vba

我正试图从这个网页上获取一些数据:

http://www.vodafone.de/privat/handys-tablets-tarife/smartphone-tarife.html

我希望在每个智能手机和每种合同类型的正确网站上都有整个表格。

设置的一次性付款: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”中不断循环。

有人知道为什么以及如何解决这个问题,也许还有另外一个想法如何从这个页面中获取所有这些数据。

提前谢谢。

2 个答案:

答案 0 :(得分:1)

而不是使用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

的解决方案一样多的资源

如果您在代理服务器后面,可以使用以下内容:

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服务器,这也可能对您有所帮助。

答案 1 :(得分:0)

而不是:

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
相关问题