Results 1 to 9 of 9

Thread: Get text from website into textbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    11

    Question Get text from website into textbox

    The code from the text i want
    HTML Code:
    <pre id="thepaste" class="prettyprint">I WANT THIS TEXT INTO TEXTBOX</pre>
    Thanks

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Get text from website into textbox

    If the site has an API, use that instead. Scraping text from a website is pretty nearly guaranteed to fail eventually, because active sites tend to change their HTML around a whole lot.

    If the ID is truly unique for the page, that would be ideal. It really should be, but it doesn't necessarily have to be. If it IS unique, then you can look for the element with that ID. You might give that a try first by looking to see how many matches you get on "thepaste", or 'id="thepaste"'. If you have only one match, that's great.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    11

    Re: Get text from website into textbox

    This is my code
    Code:
    Dim victim As HtmlElement = WebBrowser1.Document.GetElementById("thepaste")
    
            If victim IsNot Nothing Then
                Textbox1.Text = victim.InnerText
            End If
    Last edited by babypandah; May 25th, 2018 at 11:46 AM. Reason: hey

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    11

    Re: Get text from website into textbox

    Quote Originally Posted by Shaggy Hiker View Post
    If the site has an API, use that instead. Scraping text from a website is pretty nearly guaranteed to fail eventually, because active sites tend to change their HTML around a whole lot.

    If the ID is truly unique for the page, that would be ideal. It really should be, but it doesn't necessarily have to be. If it IS unique, then you can look for the element with that ID. You might give that a try first by looking to see how many matches you get on "thepaste", or 'id="thepaste"'. If you have only one match, that's great.
    It is unique

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Get text from website into textbox

    Does the code work? If not, then where is it located? After all, it is entirely possible that the element hasn't loaded by the time the code is called.
    My usual boring signature: Nothing

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Get text from website into textbox

    Quote Originally Posted by babypandah View Post
    This is my code
    Code:
    Dim victim As HtmlElement = WebBrowser1.Document.GetElementById("thepaste")
    
            If victim IsNot Nothing Then
                Textbox1.Text = victim.InnerText
            End If

    Now there is a non suspicious variable name :/

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    11

    Re: Get text from website into textbox

    The code doesn't work, if i did i didnt had a problem, but i putted in on the Webbrowser Document Load Complete event so it should be loaded

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Get text from website into textbox

    Yeah, that's a strange variable name. What does it mean?

    You would expect that LoadComplete (or is it DocumentComplete) would mean that the document has finished loading. A long time back, you would have been right. Back then, web pages were simple text with some pictures embedded. You asked for a document, you got a document. When you had finished getting the document, it was complete. Those days are long gone. Modern web pages could be as simple as that, but most are not. You will likely get several such events as different parts of a page loads.
    My usual boring signature: Nothing

  9. #9
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Get text from website into textbox

    This is part of why Shaggy Hiker said to use an API if it was available.

    In modern web apps, there's often a lot of magic that the JS wants to do to the page. But it needs the browser to be done with the page before it can do that. So many web apps load a relatively blank page, wait for the "document loaded" event, then use JS to load the *actual* content. That could be your page: it could be true that even after DocumentLoaded not all the content is there.

    There's not a "document loaded and JS has finished executing" event. There's no need for it, from a browser's viewpoint. The web wasn't designed for screen scraping. If you can't use an API to get at the data, you're going to have to look at the page's JS and find out what it's doing to load the content you want, then simulate that yourself.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

Tags for this Thread

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