0

需要解析站点播放音乐并从播放列表中获取歌曲数据

有这个表(ru版本):

<tr class="song-row " data-id="ef713e30-ea6c-377d-a1a6-bc55ef61169c" data-song-type="7" data-subscription-links="true" data-index="0"><td data-col="index"><span class="column-content">1<button aria-label="Воспроизвести: HUMBLE." data-id="play" class="aria-play-button"></button></span></td><td data-col="title"><div class="title-right-items"><iron-icon icon="av:explicit" role="img" aria-label="Не предназначено для детей" title="Не предназначено для детей" class="x-scope iron-icon-0"><svg viewBox="0 0 24 24" preserveAspectRatio="xMidYMid meet" focusable="false" class="style-scope iron-icon" style="pointer-events: none; display: block; width: 100%; height: 100%;"><g class="style-scope iron-icon"><path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-4 6h-4v2h4v2h-4v2h4v2H9V7h6v2z" class="style-scope iron-icon"></path></g></svg>

在这里,例如 - 歌曲的名称

<span class="column-content tooltip"><img src="https://lh3.googleusercontent.com/UVqWaml2fkVCgsErJIZBLeSn6LlOCI1t_W68-1HABS8dos9fWNn-4cGkboaKf-5Ro_YhthMx=s60-e100-c" alt="">HUMBLE.</span>

如何获取数据,例如歌曲名称、艺术家等?

4

1 回答 1

2

AngleSharp README 有一个非常相似的例子(参见https://github.com/AngleSharp/AngleSharp.Samples/blob/master/AngleSharp.Samples.Demos/Snippets/BigBang.cs):

// Setup the configuration to support document loading
var config = Configuration.Default.WithDefaultLoader();
// Load the names of all The Big Bang Theory episodes from Wikipedia
var address = "http://en.wikipedia.org/wiki/List_of_The_Big_Bang_Theory_episodes";
// Asynchronously get the document
var document = await BrowsingContext.New(config).OpenAsync(address);
// This CSS selector gets the desired content
var cellSelector = "tr.vevent td:nth-child(3)";
// Perform the query to get all cells with the content
var cells = document.QuerySelectorAll(cellSelector);
// We are only interested in the text - select it with LINQ
var titles = cells.Select(m => m.TextContent);

由于您没有发布与配置/ AngleSharp 相关的方法,我假设您很确定它们无论如何都可以工作......所以为了简单起见,我将省略它们。

现在,在您的情况下,这可能会转化为:

var rows = document.QuerySelectorAll("tr.song-row");
var songInfos = rows.Select(row => new {
    Title = row.QuerySelector(".column-content.tooltip")?.TextContent,
});

注意:您带来的歌曲名称示例确实看起来很笼统;我没有看到任何与歌曲名称相关的内容。因此,我假设您需要从给定行中选择特定列(例如,第 5 列)。在上面的示例中,我只是为您显示的元素使用了选择器。

于 2019-06-22T10:07:12.843 回答