C# 从网站上阅读特定文本
我试图建立一个数据库,但我需要从一个网站获得信息。主要是IMDB网站上的标题、日期、长度和体裁。我试过50种不同的方法,但都不管用。 这是我的密码C# 从网站上阅读特定文本,c#,html,xpath,html-agility-pack,C#,Html,Xpath,Html Agility Pack,我试图建立一个数据库,但我需要从一个网站获得信息。主要是IMDB网站上的标题、日期、长度和体裁。我试过50种不同的方法,但都不管用。 这是我的密码 public string GetName(string URL) { HtmlWeb web = new HtmlWeb(); HtmlDocument doc = web.Load(URL); var Attr = doc.DocumentNode.SelectNodes("//*[@id=\"ov
public string GetName(string URL)
{
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(URL);
var Attr = doc.DocumentNode.SelectNodes("//*[@id=\"overview - top\"]/h1/span[1]@itemprop")[0];
return Name;
}
当我运行这个程序时,它只会给我一个xApathException。我只想让它返回电影的标题。我现在只是以这部电影为例进行测试,但我希望它能与所有电影一起使用
我正在使用HtmlAgilityPack。我做了一些熟悉的事情,这是我从imdb.com网站获取信息的代码:
string html = getUrlData(imdbUrl + "combined");
Id = match(@"<link rel=""canonical"" href=""http://www.imdb.com/title/(tt\d{7})/combined"" />", html);
if (!string.IsNullOrEmpty(Id))
{
status = true;
Title = match(@"<title>(IMDb \- )*(.*?) \(.*?</title>", html, 2);
OriginalTitle = match(@"title-extra"">(.*?)<", html);
Year = match(@"<title>.*?\(.*?(\d{4}).*?\).*?</title>", html);
Rating = match(@"<b>(\d.\d)/10</b>", html);
Genres = matchAll(@"<a.*?>(.*?)</a>", match(@"Genre.?:(.*?)(</div>|See more)", html));
Directors = matchAll(@"<td valign=""top""><a.*?href=""/name/.*?/"">(.*?)</a>", match(@"Directed by</a></h5>(.*?)</table>", html));
Cast = matchAll(@"<td class=""nm""><a.*?href=""/name/.*?/"".*?>(.*?)</a>", match(@"<h3>Cast</h3>(.*?)</table>", html));
Plot = match(@"Plot:</h5>.*?<div class=""info-content"">(.*?)(<a|</div)", html);
Runtime = match(@"Runtime:</h5><div class=""info-content"">(\d{1,4}) min[\s]*.*?</div>", html);
Languages = matchAll(@"<a.*?>(.*?)</a>", match(@"Language.?:(.*?)(</div>|>.?and )", html));
Countries = matchAll(@"<a.*?>(.*?)</a>", match(@"Country:(.*?)(</div>|>.?and )", html));
Poster = match(@"<div class=""photo"">.*?<a name=""poster"".*?><img.*?src=""(.*?)"".*?</div>", html);
if (!string.IsNullOrEmpty(Poster) && Poster.IndexOf("media-imdb.com") > 0)
{
Poster = Regex.Replace(Poster, @"_V1.*?.jpg", "_V1._SY200.jpg");
PosterLarge = Regex.Replace(Poster, @"_V1.*?.jpg", "_V1._SY500.jpg");
PosterFull = Regex.Replace(Poster, @"_V1.*?.jpg", "_V1._SY0.jpg");
}
else
{
Poster = string.Empty;
PosterLarge = string.Empty;
PosterFull = string.Empty;
}
ImdbURL = "http://www.imdb.com/title/" + Id + "/";
if (GetExtraInfo)
{
string plotHtml = getUrlData(imdbUrl + "plotsummary");
}
//Match single instance
private string match(string regex, string html, int i = 1)
{
return new Regex(regex, RegexOptions.Multiline).Match(html).Groups[i].Value.Trim();
}
//Match all instances and return as ArrayList
private ArrayList matchAll(string regex, string html, int i = 1)
{
ArrayList list = new ArrayList();
foreach (Match m in new Regex(regex, RegexOptions.Multiline).Matches(html))
list.Add(m.Groups[i].Value.Trim());
return list;
}
string html=getUrlData(imdbUrl+“组合”);
Id=匹配(@“”,html);
如果(!string.IsNullOrEmpty(Id))
{
状态=真;
Title=match(@“(IMDb \-)*(.*?)\(.*?)”,html,2);
originaltile=match(@“title extra”“>(.*)XPath的最后一位无效。此外,要从HtmlDocument()
仅获取单个元素,可以使用SelectSingleNode()
而不是SelectNodes()
:
输出:
The 40-Year-Old Virgin
演示链接:*
*)演示显示打印了正确的标题,以及一个特定于.NET Fiddle的错误(您可以安全地忽略该错误).非常感谢,我在过去的两年里一直在努力days@kevindt12不客气,我感谢你努力学习这些东西。顺便说一句,别忘了接受答案:你能帮我确定发布日期和图片吗?@kevindt12现在你手上有一个工作示例。唯一需要更改的部分就是获得其他部分r数据,即发布日期和图片是xpath
。请在internet的帮助下自己再试一次。@kevindt12以后-不一定是2天后:)-如果您仍然无法得到正确的结果,请发布一个新问题,显示您尝试过的XPath,我将非常乐意帮助您。看起来很有趣,我不了解其中的一些,但很有趣。我是新来的,正在练习C#我已经加载了这个类:并且稍微进行了编辑以适合我。
The 40-Year-Old Virgin