Get data out of a webpage with VBA

馋奶兔 提交于 2020-01-30 10:31:41

问题


I'm trying to get some data out of this Webpage:

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

I want to have the whole table on the right site for every Smartphone and every contract type.

one time payment for Setting up: Anschlusspreis

one time payment for the phone: Smartphone

total amount: Gesamt

Monthly payment for the contract: Basispreis

Monthly payment for the mobile phone: Smartphone-Zuzahlung

This is all stored in the JavaScript part which is a huge amout of letters.

I´m trying to use 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

But it is continuously Looping at "DoEvents".

Does someone have an idea why and how I can solve this and maybe another idea how to get all this data out of this page.

Thank you in advance.


回答1:


instead of using the IE automation you can also use a http request object:

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

it's faster and doesn't need as many ressources as the solution with the IE

if you are behind a proxy server you can use something like this:

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

of course you have to adjust the proxy to your values.

As you are interessted in a german page here also a short explanation in german language: http://cboden.de/softwareentwicklung/vba/tipps-tricks/27-webseite-per-vba-ansprechen There is also explained how to pass values of a form to the webserver which might also be helpfull for you.




回答2:


Instead of:

Do
    DoEvents

Loop Until ie.readyState = READYSTATE_COMPLETE  

You could try:

Do While ie.Busy: DoEvents: Loop
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

with ie
...
end with


来源:https://stackoverflow.com/questions/26613043/get-data-out-of-a-webpage-with-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!