无法转换类型“字符串”;到“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));
}
}
}
如何将下载的字符串转换为有效的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该适用于 v1.4:
或
This should work with v1.4:
or
试试这个(基于 这个相当古老的文档):
基本上,转换很少是两种类型之间转换的正确方法 - 特别是当正在进行解析之类的事情时。
Try this (based on this fairly old document):
Basically casting is rarely the right way of converting between two types - particularly when there's something like parsing going on.
以下代码行将使用您的内容创建一个
HtmlDocument
:The following lines of code will create a
HtmlDocument
with your content:也许您可以在文件系统中创建一个新文件(.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