从网站获取数据

时间:2014-07-09 17:28:28

标签: windows-phone-7 windows-phone-8 webclient

我正在一个WindowsPhone项目工作,我想从网站上获取数据,例如IMDb。

所以我用IMDb页面下载了html

private void addButton_Click(object sender, RoutedEventArgs e)
    {
        WebClient webclient = new WebClient();
        webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webclient_DownloadStringCompleted);
        webclient.DownloadStringAsync(new Uri("http://www.imdb.com/title/tt2294449/"));
    }

    void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            MessageBox.Show("error");
        }
        string html = e.Result;
    }

到目前为止它的确有效。

现在我想在这个html中搜索电影的de title。最好的方法是什么?

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

另一种方法是使用正则表达式手动解析HTML,但是由于HTML代码的不规则性,使用HTML Agility Pack等HTML解析库会更好(并且可能会减少错误!)

答案 2 :(得分:0)

获取Html Agility Pack

这里有一些代码可以帮助您入门(需要错误检查)

HtmlDocument document = new HtmlDocument(); 
string htmlString = "<html>blabla</html>";
document.LoadHtml(htmlString);
HtmlNodeCollection collection = document.DocumentNode.SelectNodes("//a");
foreach (HtmlNode link in collection)
{
     string target = link.Attributes["href"].Value;
}
相关问题