【问题标题】:Reading Specific text from a website从网站读取特定文本
【发布时间】:2016-03-11 01:56:59
【问题描述】:

我正在尝试创建数据库,但我需要从网站获取信息。主要是来自 IMDB 网站的标题、日期、长度和类型。我已经尝试了 50 种不同的东西,但它不起作用。 这是我的代码。

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

当我运行它时,它只会给我一个 XPathException。我只想让它返回电影的标题。我现在只是将这部电影用作示例和测试,但我希望它适用于所有电影http://www.imdb.com/title/tt0405422 我正在使用 HtmlAgilityPack。

【问题讨论】:

    标签: c# html xpath html-agility-pack


    【解决方案1】:

    XPath 的最后一位无效。另外要从 HtmlDocument() 获取单个元素,您可以使用 SelectSingleNode() 而不是 SelectNodes()

    HtmlWeb web = new HtmlWeb();
    HtmlDocument doc = web.Load("http://www.imdb.com/title/tt0405422/");
    
    var xpath = "//*[@id='overview-top']/h1/span[@class='itemprop']";
    var span = doc.DocumentNode.SelectSingleNode(xpath);
    var title = span.InnerText;
    
    Console.WriteLine(title);
    

    输出:

    The 40-Year-Old Virgin
    

    演示链接: *

    https://dotnetfiddle.net/P7U5A7

    *) 演示显示打印了正确的标题,以及特定于 .NET Fiddle 的错误(您可以放心地忽略该错误)。

    【讨论】:

    • 非常感谢这两天我一直在努力
    • @kevindt12 不客气,感谢您为学习这些东西所做的努力。顺便说一句,不要忘记接受答案:stackoverflow.com/help/someone-answers
    • 你能帮我看看发布日期和图片吗?
    • @kevindt12 现在你手头有一个工作示例。唯一需要更改以获取其他数据(即发布日期和图片)的部分是xpath。在互联网的帮助下自己再试一次。
    • @kevindt12 稍后 - 不一定是 2 天后 :) - 如果您仍然无法获得正确的结果,请发布一个新问题,显示您尝试过的 XPath,我会非常高兴帮助
    【解决方案2】:

    我做了一些熟悉的事情,这是我从 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;
        }
    

    也许你会发现一些有用的东西

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    相关资源
    最近更新 更多