3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using HtmlAgilityPack;

namespace sss
{
    public class Downloader
    {
        WebClient client = new WebClient();

        public HtmlDocument FindMovie(string Title)
        { 
            //This will be implemented later on, it will search movie.
        }

        public HtmlDocument FindKnownMovie(string ID)
        {
            HtmlDocument Page = (HtmlDocument)client.DownloadString(String.Format("http://www.imdb.com/title/{0}/", ID));
        }
    }
}

How can I convert a downloaded string to a valid HtmlDocument so I can parse it using HTMLAgilityPack?

4 Answers 4

7

This should work with v1.4:

HtmlWeb hw = new HtmlWeb();
HtmlDocument doc = hw.Load(string.Format("http://www.imdb.com/title/{0}/", ID));

or

string html = client.DownloadString(String.Format("http://www.imdb.com/title/{0}/", ID));
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
Sign up to request clarification or add additional context in comments.

Comments

5

Try this (based on this fairly old document):

string url = String.Format("http://www.imdb.com/title/{0}/", ID);
string content = client.DownloadString(url);
HtmlDocument page = new HtmlDocument();
page.LoadHtml(content);

Basically casting is rarely the right way of converting between two types - particularly when there's something like parsing going on.

Comments

1

The following lines of code will create a HtmlDocument with your content:

// First create a blank document
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
// Then load it with the content from the webpage you are trying to parse
doc.Load(new StreamReader(WebRequest.Create("yourURL").GetResponse()
                                 .GetResponseStream()));

Comments

0

Maybe you could create a new file (.html) in for file system and then use a stream writer to write the string into the html file. Then pass that file to the parser

Comments

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.