无法转换类型“字符串”;到“HtmlAgilityPack.HtmlDocument”?

发布于 2024-09-05 06:29:38 字数 664 浏览 4 评论 0原文

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));
        }
    }
}

如何将下载的字符串转换为有效的 HtmlDocument,以便可以使用 HTMLAgilityPack 对其进行解析?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

棒棒糖 2024-09-12 06:29:38

这应该适用于 v1.4:

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

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

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);
青芜 2024-09-12 06:29:38

试试这个(基于 这个相当古老的文档):

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

基本上,转换很少是两种类型之间转换的正确方法 - 特别是当正在进行解析之类的事情时。

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.

﹂绝世的画 2024-09-12 06:29:38

以下代码行将使用您的内容创建一个 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()));

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()));
乱了心跳 2024-09-12 06:29:38

也许您可以在文件系统中创建一个新文件(.html),然后使用流写入器将字符串写入 html 文件。然后将该文件传递给解析器

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文