-1

I have searched for similar "403" threads but could not find an answer.

I have the following URL which is valid and working - www.investing.com/equities/apple-computer-inc-earnings

And my code:

Sub GetSourceCode()

    Dim XMLHTTP As Object
    Dim URL As String
    Dim data As String

    URL = "http://www.investing.com/equities/apple-computer-inc-earnings"
    Set XMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    XMLHTTP.Open "GET", URL, False
    XMLHTTP.send
    data = XMLHTTP.responseText
End Sub

However, The string "data" is returned with a "403 Forbidden" error instead of the source code, which is what I am trying to achieve.

Any solution or a workaround (or a duplicate reference) would be highly appreciated.

Thanks

Community
  • 1
  • 1
David912
  • 378
  • 1
  • 2
  • 11
  • 1
    What are you trying to retrieve into this string? You are certainly not expecting to get everything you see on the website into this string. May I suggest that you have a look at this solution and give it a try: http://stackoverflow.com/questions/8798260/html-parsing-of-cricinfo-scorecards/8846791#8846791 Even if this solution is not usable it might at least help you better identify the item(s) you wish to retrieve from the website. – Ralph May 16 '16 at 21:00
  • Thanks I will look into that but I also want to add that if the URL is replaced with URL = "http://www.google.com" it seems to work as expected and the source code is stored entirely in the string. Of course I am only interested in a small piece of data from that string but first I must download it completely, no? – David912 May 16 '16 at 21:15
  • Short answer: no, you don't have to download the entire website. – Ralph May 16 '16 at 21:20
  • 1
    The host probably doesn't want their data being scraped :) – SierraOscar May 16 '16 at 21:55
  • Ralph, I tried to use the code in the link you provided (the 1st technique) but the compiler stops me when it gets to "Dim tbl As HTMLTable" since it is undefined user type... any thoughts? – David912 May 16 '16 at 22:25
  • ...oh I missed the library reference which is needed... should have paid more attention. now it seems to work and I am able to parse what I need. – David912 May 17 '16 at 10:51

1 Answers1

1

set request header just before .send

XMLHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
Pawel
  • 891
  • 1
  • 9
  • 31