1

I'm trying to make an application where it will read certain texts from a website.

using AngleSharp.Parser.Html;
...
            var source = @"
            <html>
            <head>
            </head>
            <body>
            <td class=""period_slot_1"">
            <strong>TG</strong>
            </body>
            </html>";

        var parser = new HtmlParser();
        var document = parser.Parse(source);
        var strong = document.QuerySelector("strong");

        MessageBox.Show(strong.TextContent); // Display text

From googling, I've successfully done above. I have copy&pasted a part of html in a variable to see if I can get the value I'm looking for. So it gets the value I want, which is string "TG".

However, the website will have different value to "TG" every time, so I need my program to refer straight to the html of the website at the time.

Is is possible for me to load the whole html source in the source variable and make it work, if can how can I do it and what would be best for me to get what I want?

Thank you so much for reading the question.

1 Answer 1

2

I assume you're saying you want to read directly from a page on the internet from a url. In which case you should do:

        WebClient myClient = new WebClient();
        Stream response = myClient.OpenRead("http://yahoo.com");
        StreamReader reader = new StreamReader(response);
        string source = reader.ReadToEnd();
        var parser = new HtmlParser();
        var document = parser.Parse(source);
        var p = document.QuerySelector("p");    
        // I used 'p' instead of 'strong' because there's no
        //strong on that page
        MessageBox.Show(p.TextContent); // Display text
        response.Close();
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer! :)
May I please ask a question? I have used a webbrowser object to load the webpage, and log in. With your help I can load url, but when I put the url of the webpage it reads html of a page where it's not logged in yet. Do you know how i can get html of the page that's logged in?
I don't know if I can answer that. It depends a lot on how the login process works on that particular page. There should be some kind of http request that would log your Webclient in, but it would probably be a Post instead of a Get method. You can probably figure out what the request would be by looking at the source of the login form and seeing what parameters it sends the login information it, but that's going to be different for every page.
Thank you for your reply! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.