Results 1 to 4 of 4

Thread: Using InStr to Search for Words on Web Page?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2008
    Location
    St. John's, Newfoundland, Canada
    Posts
    965

    Using InStr to Search for Words on Web Page?

    If I were to key in a word like "automobile" and open the first page for that particular word on Thesaurus.com, and then search within that web page in the Synonyms section for any synonyms, and then print all those synonyms in the Synonyms section on a line on a text file (with a line separator "|"), what would I have to do?

    Of course, I don't want all the words in Thesaurus.com. I only want select words that I intend to use for a Family Feud game I am developing.

  2. #2
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Using InStr to Search for Words on Web Page?

    There are a couple of ways I can think of off the top of my head. One would be to use the "getelementbyid" property, which would require you to have a webbrowser control on your form... probably Visible = False. The other way (which I actually prefer) is to ghost download the page and search through it via that. That way, you're downloading text and it would be easier to search using Instr for what you want to extract.

    Here is the code for the ghost download:

    Form Declarations
    Code:
    Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
        "URLDownloadToFileA" (ByVal pCaller As Long, _
        ByVal szURL As String, _
        ByVal szFileName As String, _
        ByVal dwReserved As Long, _
        ByVal lpfnCB As Long) As Long
    
    Public Function DownloadFile(URL As String, _
        LocalFilename As String) As Boolean
        Dim lngRetVal As Long
        lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
        If lngRetVal = 0 Then DownloadFile = True
    End Function
    Call like this:
    Code:
    DownloadFile "http://thesaurus.com/browse/automobile", "c:\Downloads\thes_automobile.htm"
    Then search through it like this
    Code:
    Open "c:\Downloads\thes_automobile.htm" For Input As 1
    Dim U As String
            While EOF(1) = 0
            Line Input #1, U
    'Search using Instr here... U is the string it's currently getting
    Wend
    Close #1
    Hope this helps.

  3. #3
    Hyperactive Member Rattled_Cage's Avatar
    Join Date
    Dec 2005
    Posts
    315

    Lightbulb Re: Using InStr to Search for Words on Web Page?

    another way without downloading each page is to add Microsoft html object libary >>in Project References


    and use the Function in this

    Code:
    Private Sub Command1_Click()
        Dim strfindword As String
            strfindword = "Rattled"
                If WebPageContains(strfindword) = True Then 'check if the word is in page
                   ''' MsgBox "The webpage contains the text" 'string is in page
                MsgBox strfindword & "Found"
                Else
                MsgBox "The webpage doesn't contains the text" 'string is not in page
                End If
    End Sub
    Private Sub Form_Load()
    WebBrowser1.Navigate "http://www.vbforums.com/showthread.php?746939-Using-InStr-to-Search-for-Words-on-Web-Page"
    End Sub
    Private Function WebPageContains(ByVal s As String) As Boolean
        Dim I As Long, EHTML
        For I = 1 To WebBrowser1.Document.All.length
            Set EHTML = _
            WebBrowser1.Document.All.Item(I)
            If Not (EHTML Is Nothing) Then
                If InStr(1, EHTML.innerHTML, _
                s, vbTextCompare) > 0 Then
                WebPageContains = True
                Exit Function
            End If
        End If
    Next I
    End Function

  4. #4
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Using InStr to Search for Words on Web Page?

    I'm sure he could do either way, yet I don't see how he would know what words to search for. If he plans on using hundreds of words, being specific about each entry is going to be ridiculously time-consuming. Perhaps if he were just wanting a certain number of words, he could cut off the search at, say, 5 entries found. When he says "I only want select words", I'm not sure he would know which words would appear on the page, especially if he's using hundreds of primary words to search for. Now, if he meant "I only want a certain number of words returned", that would seem more logical. Perhaps if the OP could embellish on how he plans to choose which "select words" he wants, that could help us help him.

Posting Permissions

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



Click Here to Expand Forum to Full Width