我试图通过返回html的特定部分来控制AngleSharp。
到目前为止,这是我的代码:
using (WebClient client = new WebClient())
{
string htmlCode = client.DownloadString("http://www.planetradio.de/music/trackfinder.html");
var parser = new HtmlParser();
var document = parser.Parse(htmlCode);
var blueListItemsLinq = document.All.Where(m => m.LocalName == "span id" && m.ClassList.Contains("headerTracklistCurrentSongArtist"));
foreach (var item in blueListItemsLinq)
label.Content = item;
}
我想要它返回的是当前的艺术家,它应该在html下面:
<div id="headerTracklistCurrentSong">
<span id="headerTracklistCurrentSongArtist"><a href="music/trackfinder.html">Olly Murs</a></span>
<span id="headerTracklistCurrentSongTitle"><a href="music/trackfinder.html">Kiss Me</a></span>
但我似乎犯了一个错误....所以如果有人可以帮助我并向我解释,我会很高兴...
先谢谢大家回答。 :)
答案 0 :(得分:2)
span元素被解析为AngleSharp.Dom.Html.HtmlSpanElement
所以你的查询应该是:
var blueListItemsLinq = document.All.Where(m => m.LocalName == "span" && m.Id == "headerTracklistCurrentSongArtist");
然后你可以得到这样的文字/值:
foreach (var item in blueListItemsLinq)
{
label.Content = item.TextContent; // "Olly Murs"
var child = item.FirstChild as AngleSharp.Dom.Html.IHtmlAnchorElement;
var text = child.Text; // "Olly Murs"
var path = child.PathName; // "/music/trackfinder.html"
}
<强>已更新强>
由于currnt Artist名称显示在&#34; planetradio.de/music/trackfinder.html"的表格中,您可以得到如下名称:
var hitfinderTable = document.All.Where(m => m.Id == "hitfindertable").First() as AngleSharp.Dom.Html.IHtmlTableElement;
foreach (var row in hitfinderTable.Rows)
{
var artistName = row.Cells[2].TextContent;
}