gpt4 book ai didi

c# - 使用 CsQuery 遍历 dom

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:17 32 4
gpt4 key购买 nike

我正在尝试学习如何使用 CsQuery 遍历 dom 以获取特定文本。

html 看起来像这样:

<div class="featured-rows">
<div class="row">
<div class="featured odd" data-genres-filter="MA0000002613">
<div class="album-cover">
<div class="artist">
<a href="http://www.allmusic.com/artist/half-japanese-0000555654">Half apanese</a>
</div>
<div class="title">
<div class="label"> Joyful Noise </div>
<div class="styles">
<div class="rating allmusic">
<div class="rating average">
<div class="headline-review">
</div>
<div class="featured even" data-genres-filter="MA0000002572, MA0000002613">
</div>
<div class="row">
<div class="row">
<div class="row">

我的代码尝试如下所示:

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 来获取文本。

最佳答案

你应该尝试类似这样的事情:

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
}

关于c# - 使用 CsQuery 遍历 dom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25665928/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com