0

我有var source="<p><a href="http://in.news.yahoo.com/googles-stock-split-raises-questions-023232813.html"><img src="http://l.yimg.com/bt/api/res/1.2/TRLtYhdbTvFcX_GOU_0S4g--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9ODU7dz0xMzA-/http://media.zenfs.com/en_us/News/Reuters/2012-04-14T023232Z_5_CBRE83B1MAL00_RTROPTP_2_USA.JPG" width="130" height="86" alt="People visit Google's stand at the National Retail Federation Annual Convention and Expo in New York" align="left" title="People visit Google's stand at the National Retail Federation Annual Convention and Expo in New York" border="0" /></a>(Reuters) - An unusual stock split designed to preserve Google Inc founders' control of the Web search leader raised questions and some grumbling on Wall Street, even as investors focused on the company's short-term business concerns. Shares of Google closed 4 percent lower at $624.60 on Friday, driven by deepening worries about its search ad rates and payments to partners. The declining search trends underscored investor uncertainty about Google's growth prospects and unease about the company's pending $12.5 billion acquisition of Motorola Mobility. ...</p><br clear="all"/>" 现在我需要解析/抓取它以获取变量中的链接地址,即http://in.news.yahoo.com/googles-stock-split-raises-questions-023232813.html在单独的变量中获取图像 src。</a>我还需要和..之间的描述文本</p>。请帮助我严重卡住...

4

1 回答 1

1

使用 HtmlAgilityPack 尝试以下代码片段

var source = @"<p><a href=""http://in.news.yahoo.com/googles-stock-split-raises-questions-023232813.html""><img src=""http://l.yimg.com/bt/api/res/1.2/TRLtYhdbTvFcX_GOU_0S4g--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9ODU7dz0xMzA-/http://media.zenfs.com/en_us/News/Reuters/2012-04-14T023232Z_5_CBRE83B1MAL00_RTROPTP_2_USA.JPG"" width=""130"" height=""86"" alt=""People visit Google's stand at the National Retail Federation Annual Convention and Expo in New York"" align=""left"" title=""People visit Google's stand at the National Retail Federation Annual Convention and Expo in New York"" border=""0"" /></a>(Reuters) - An unusual stock split designed to preserve Google Inc founders' control of the Web search leader raised questions and some grumbling on Wall Street, even as investors focused on the company's short-term business concerns. Shares of Google closed 4 percent lower at $624.60 on Friday, driven by deepening worries about its search ad rates and payments to partners. The declining search trends underscored investor uncertainty about Google's growth prospects and unease about the company's pending $12.5 billion acquisition of Motorola Mobility. ...</p><br clear=""all""/>";

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(source);

var paraNode = doc.DocumentNode.SelectSingleNode("//p");
var desc = paraNode.InnerText;

var anchorNode = doc.DocumentNode.SelectSingleNode("//p/a");
var link = anchorNode.GetAttributeValue("href", null);

var imgNode = doc.DocumentNode.SelectSingleNode("//p/a/img");
var src = imgNode.GetAttributeValue("src", null);

有很多方法可以做到这一点,但这只是完成工作的方法之一。它让您知道如何使用HtmlAgilityPack. XPATH在解析这样的东西时会给你很大的力量。

于 2012-04-15T18:33:27.630 回答