• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Parsing certain fields from html elements saved in a text file using vba

shahin

Active Member
Hi there!! Hope you all are doing well. For testing any vba script, is it possible to use a text file? I meant, I have a text file in which certain html elements are stored. I would like to parse my preferable fields from that elements using vba. As it is not required to use "http" request in this case, so I can't find any idea how to move along. If you see below you can find an html snippet which is saved in a "movie.txt" file. I would like to parse the "movie" name and "year" from this using vba. Any help will be greatly appreciated. Thanks in advance.
Code:
<html>
<div class="browse-movie-wrap col-xs-10 col-sm-4 col-md-5 col-lg-4">
    <a href="https://yts.ag/movie/baby-driver-2017" class="browse-movie-link">
    <figure>
    <img class="img-responsive" src="https://yts.ag/assets/images/movies/baby_driver_2017/medium-cover.jpg" alt="Baby Driver (2017) download" width="170" height="255">
    <figcaption class="hidden-xs hidden-sm">
    <span class="icon-star"></span>
    <h4 class="rating">8 / 10</h4>
    <h4>Action</h4>
    <h4>Crime</h4>
    <span class="button-green-download2-big">View Details</span>
    </figcaption>
    </figure>
    </a>
    <div class="browse-movie-bottom">
    <a href="https://yts.ag/movie/baby-driver-2017" class="browse-movie-title">Baby Driver</a>
    <div class="browse-movie-year">2017</div>
        <div class="browse-movie-tags">
            <a href="https://yts.ag/torrent/download/A89A0E14FD42615E4CEC382A4225A7A7C5622BE2" rel="nofollow" title="Download Baby Driver 720p Torrent">720p</a>
            <a href="https://yts.ag/torrent/download/3676634E0B750299F5CF31E52B2AD035DF641ACE" rel="nofollow" title="Download Baby Driver 1080p Torrent">1080p</a>
        </div>
    </div>
</div>
</html>
 
Last edited:
Hi !

As you already see within my codes
just with VBA basics text functions or using htmlfile ActiveX …
 
It's a long long long ----time since I have the opportunity to interact with you. Thanks for your availability. However, will it be possible to parse certain fields using regular method, as in ".getElementsByClassName" etc? If it is then I would be very glad to have a link of that thread where you have used this. Thanks again.
 
Just read content of text file into string variable. And load it to HTMLDoc much like you do it with XML.responseText

To read text file content...

Code:
Sub Demo()
Dim html As Object
Dim fPath As String: fPath = "C:\test\freedom.txt"
Dim strContent As String
Dim intFF As Integer: intFF = FreeFile()
Open fPath For Input As #intFF
strContent = Input(LOF(intFF), intFF)
Close #intFF

Set html = CreateObject("htmlfile")

html.body.innerHTML = strContent

Debug.Print html.body.innerHTML

End Sub
 
Thanks sir Chihiro, for such an incredible demo. This is exactly I was expecting to have. Btw, is it possible that I can use early binding in case of omitting "Set html = CreateObject("htmlfile")" this. If it is then what reference i should add to the library? Thanks sir.
 
Oh my god!!! I still can't believe that it is possible in the way i usually do. It is working in both ways. Thanks again sir.
 
"Set html = CreateObject("htmlfile")" this. If it is then what reference i should add to the library? Thanks sir.

As you probably found out. MS HTML Object Library. For quick demo I didn't feel like going into tool and adding reference to the library ;)
 
Back
Top