Can't convert type 'string' to 'HtmlAgilityPack.HtmlDocument'?

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 to convert a loaded string to a valid HtmlDocument so that I can parse it using HTMLAgilityPack?

+3
source share
4 answers

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);
+6
source

Try this (based on this pretty 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 to convert two types - especially when something like parsing happens.

+4
source

HtmlDocument :

// 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()));
+1

, (.html) , html .

0

Source: https://habr.com/ru/post/1749611/


All Articles