使用CsQuery遍历dom
我正在尝试学习如何使用CsQuery遍历dom以获取特定文本。
html看起来像这样:
Joyful Noise
我的代码尝试看起来像这样:
public void GetRows() { var artistName = string.Empty; var html = GetHtml("http://www.allmusic.com/newreleases"); var rows = html.Select(".featured-rows"); foreach(var row in rows) { var odd = row.Cq().Find(".featured odd"); foreach(var artist in odd) { artistName = artist.Cq().Text(); } } }
.featured-row的第一个选择有效,但后来我不知道如何找到.artist来获取文本。
List artists = html[".featured .artist a"].Select(dom=>dom.TextContent).ToList();
其中html ==你的CQ对象。
var odd = row.Cq().Find(".featured odd");
应该
var odd = row.Cq().Find(".featured.odd");
你应该尝试类似的东西:
var html = GetHtml("http://www.allmusic.com/newreleases"); var query = CQ.Create(html) var row = query[".artist>a"]; string link = row.Attributes["href"]; string text = row.DefaultValue or row.InnerText or row.Value...
CsQuery是JQuery的端口,因此您可以谷歌搜索JQuery代码
更新:遍历以获得所有艺术家和头衔
var rows = query[".featured odd"]; foreach(var row in rows) { var artistsLink = row[".artists>a"]; var title = row[".title"]; // here do whatever you need with this }
Interesting Posts