+ Reply to Thread
Results 1 to 26 of 26

Getting Web page source (HTML or XML) in VBA?

Hybrid View

  1. #1
    Registered User
    Join Date
    10-27-2006
    Posts
    18

    Getting Web page source (HTML or XML) in VBA?

    I would like to retrieve contents of a web page, be it HTML or XML, into VBA variable!

    Later, I would chop, cut, parse or extract the data I need.

    Both importing as XML or WebQueries is unsatisfactory for a certain number of pages I need. XML has bad schema, WebQuery tells me it can't find any data.

    I tried with WinHTTPRequest, but Excel gives me back error "undefined user type" in other words it doesn't recognize that object.

    Basically I want the source of web page to become a string in my VBA code. In other words that would be replication of funcionallity of
    urldownloadtofile http://anywebpage.com/index.html anyfile.txt
    which is provided by AutoHotKey scripting language (if anyone used it, very simple and effective for many things).

    Can it be done in Excel's VBA?

  2. #2
    Registered User
    Join Date
    10-27-2006
    Posts
    18
    I get no takers on this one?

    Come on, this is chalenging, it's not like "How do I display a message box before deleting something?" or "How do I make cell A4 red?". Not that I don't think these don't deserve replies, they just have standard answers.

  3. #3
    Forum Contributor
    Join Date
    06-27-2006
    Posts
    310
    I've done some related stuff ,but I never found a way to download the source file.

    I did end up of having success downloading tables where I could reference individual elements in tables , count how many tables were on a page etc.

    Prior to that I was also seeking to download the source file, but abandoned that route once I able to selectively pick my data directly off the webpage.

    If you think it can be any help I can probably dig up an example.

  4. #4
    Registered User
    Join Date
    10-27-2006
    Posts
    18
    Thanks SuitedAces (are they red or black?)!

    The very reason I'm looking to download source is because using WebQuery to retrieve data fails to retrieve data from pages I need. I don't know the reason why is that so.

    Example:
    http://www.wagerline.com/Scores-Odds...ontID=9361&t=0

    Site displaying various betting and sports info. In this particular example we have a boxscore which has perfectly formatted HTML as source.

    You can verify that tables are really present by looking at HTML source or by using WebDeveloper add-on for Firefox which can display information on just about any part of the web page! This particular page has 5 of them: login box, two team statistics, technical fouls and team statistics.

    Why Excel's WebQuery can't pick any of them - I have no idea (it gives something like "connection is there, but there are no data"). That's why I need the source to parse out tables myself.

  5. #5
    Forum Contributor
    Join Date
    06-27-2006
    Posts
    310
    Quote Originally Posted by Riorin
    Thanks SuitedAces (are they red or black?)!

    The very reason I'm looking to download source is because using WebQuery to retrieve data fails to retrieve data from pages I need. I don't know the reason why is that so.

    Example:
    http://www.wagerline.com/Scores-Odds...ontID=9361&t=0

    Site displaying various betting and sports info. In this particular example we have a boxscore which has perfectly formatted HTML as source.

    You can verify that tables are really present by looking at HTML source or by using WebDeveloper add-on for Firefox which can display information on just about any part of the web page! This particular page has 5 of them: login box, two team statistics, technical fouls and team statistics.

    Why Excel's WebQuery can't pick any of them - I have no idea (it gives something like "connection is there, but there are no data"). That's why I need the source to parse out tables myself.
    I don't understand the question , red or black ?
    Please explain , it's probably an expression I'm not familiar with.

    I encountered identical problems with WebQuery , it's very limited and problematic and my first thought was the same as yours ...download the source code and parse it.
    I haven't been able to find a way to do that .

    But hacking my way through the properties of the html doc (I don't know a damn thing about html) I was able to write procedures that enabled me to extract everything I need.

    In fact I believe I have a closely related example to yours...downloading hockey scores.
    But with that link you gave me, that might be a whole different animal because I wasn't able to look at the source file from the menu.
    Last edited by SuitedAces; 01-20-2008 at 03:53 PM.

  6. #6
    Forum Contributor
    Join Date
    06-27-2006
    Posts
    310
    Ok, check that , I WAS able to open the source file the second time I tried.
    That was strange.

  7. #7
    Registered User
    Join Date
    03-18-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Getting Web page source (HTML or XML) in VBA?

    I am trying to automate a Web query that pastes the data on a spreadsheet.
    It seems I am 95% there however yet an inch is as good as a mile.

    I need to step thru several web pages. I have a cell on the worksheet that creates the new wed address but a just need a command that will use the web address on the worksheet.

    As an example the cell P10 formula = http://www.thenewwebaddress.com./1950.xml

    Every new web address changes the _195?.xml to say _1951.xml then 1952.xml etc.

    I'm using this code I found on this site.

    _____________________
    Const MyUrl As String = Activesheet.cells("P10").String <<problem child

    With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;" & MyUrl, Destination:=Cells(1, 1))
    _____________________

    Can anyone advise the code I need to use to grab the changed wed address from cell P10 on the worksheet?

    Thanks

  8. #8
    Registered User
    Join Date
    03-18-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Getting Web page source (HTML or XML) in VBA?

    Making progress however every time I run the query again it pastes the new date X rows to the right. Is there a true/false I need to change?

    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlEntirePage
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False

  9. #9
    Registered User
    Join Date
    08-04-2010
    Location
    Portland, Oregon, USA
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Getting Web page source (HTML or XML) in VBA?

    Brilliant code, SuitedAces!

    Working off your code, I was able to turn it into an automated share count extractor from Yahoo Finance (soon to port it to Google Finance as well). Sheet2 now has a list of ~500 stock symbols in column A, and column B is being filled in one by one (~4 seconds/stock symbol) with the respective share count. It's anything but robust at this point, but it's handling the job flawlessly.

    It uses two loops - one to check each of the cells of coded output for the correct piece of info, and one to run through the list of symbols, changing the URL each time.

    If anyone would like this code, it's attached...
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    04-10-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Getting Web page source (HTML or XML) in VBA?

    how to get the data from a particular tag in website?

  11. #11
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Getting Web page source (HTML or XML) in VBA?

    Great coding and kind of along the lines of what i've been looking for - but is there a fix regarding the URL - ie - can this be called from cell A2 as originally designed?

  12. #12
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Getting Web page source (HTML or XML) in VBA?

    cmb80,

    This is an old thread. Its best if you discuss this in a new thread or in your own thread.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  13. #13
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Getting Web page source (HTML or XML) in VBA?

    I have started a new thread but no responses

  14. #14
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Getting Web page source (HTML or XML) in VBA?

    You can bump it if you do not get a reply after 12 hrs of starting the thread.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1