Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查找h2标记HtmlAgilityPack后的斜体段落文本_C#_Html_Html Agility Pack - Fatal编程技术网

C# 查找h2标记HtmlAgilityPack后的斜体段落文本

C# 查找h2标记HtmlAgilityPack后的斜体段落文本,c#,html,html-agility-pack,C#,Html,Html Agility Pack,我想抓取所有斜体和粗体的文本(歌曲名称),毕竟h2标签。下面是html代码 <h2>"Artist Name 1"></h2> <p><br><b><i>Song Name 1</i>.</b> 2008. <br><b>Music</b> Name Name <br><b>Lyrics:</b> Name Name &

我想抓取所有斜体和粗体的文本(歌曲名称),毕竟h2标签。下面是html代码

<h2>"Artist Name 1"></h2>

<p><br><b><i>Song Name 1</i>.</b> 2008.
<br><b>Music</b> Name Name
<br><b>Lyrics:</b> Name Name

<p><b><i>Song Name 2</i></b> 2008.
<br><b>Music</b> Name Name
<br><b>Lyrics:</b> Name Name

<h2>"Artist Name 2"></h2>

<p><br><b><i>Song Name 1</i>.</b> 2009.
<br><b>Music</b> Name Name
<br><b>Lyrics:</b> Name Name

<p><b><i>Song Name 2</i></b> 2009.
<br><b>Music</b> Name Name
<br><b>Lyrics:</b> Name Name

...

我可以抓取所有h2文本(艺术家姓名)。但是我需要一些帮助来继续代码

很简单,我改变了这一点:

var headers = doc.DocumentNode.SelectNodes("//h2");
为此:

 var headers = doc.DocumentNode.SelectNodes("//b/i");
它经过测试,是wokrs

仅获取歌曲名称:

结果:

Song name 1

Song name 2
如果你想要更复杂的东西,比如艺术家的名字和他/她的歌。这是工作

    var headers = doc.DocumentNode.SelectNodes("//h2");

    if (headers != null)
    {
        foreach (HtmlNode item in headers)
        {
            Console.WriteLine(item.InnerText); //Artist Name

            var next = item.NextSibling;

            while (next != null)
            {
                if (next.FirstChild != null && next.FirstChild.Name == "i")
                {
                    Console.WriteLine(next.InnerText); //Song Name for artist
                }

                if (next.Name == "h2")
                {
                    break;
                }

                next = next.NextSibling;
            }
        }
    }
结果是:


很简单,我改变了这一点:

var headers = doc.DocumentNode.SelectNodes("//h2");
为此:

 var headers = doc.DocumentNode.SelectNodes("//b/i");
它经过测试,是wokrs

仅获取歌曲名称:

结果:

Song name 1

Song name 2
如果你想要更复杂的东西,比如艺术家的名字和他/她的歌。这是工作

    var headers = doc.DocumentNode.SelectNodes("//h2");

    if (headers != null)
    {
        foreach (HtmlNode item in headers)
        {
            Console.WriteLine(item.InnerText); //Artist Name

            var next = item.NextSibling;

            while (next != null)
            {
                if (next.FirstChild != null && next.FirstChild.Name == "i")
                {
                    Console.WriteLine(next.InnerText); //Song Name for artist
                }

                if (next.Name == "h2")
                {
                    break;
                }

                next = next.NextSibling;
            }
        }
    }
结果是:


“艺术家姓名2”>
-这似乎是一个错误的HTML。你确定吗?@PraveenKumar有个错误。请忽略它。
“艺术家名称2”>
-这似乎是一个错误的HTML。你确定吗?@PraveenKumar有个错误。请不要理它,对不起。我忘了说。我想把“歌曲”分类在“艺术家”下。(例如:艺人名称1-歌曲名称1,歌曲名称2艺人名称2歌曲名称1,歌曲名称2)这就是我认为使用for循环的原因。你能增强代码来做这件事吗requirement@JumRemdesk那一刻,pls@JumRemdesk好的,我更新我的答案,solusion是wokring,很抱歉。我忘了说。我想把“歌曲”分类在“艺术家”下。(例如:艺人名称1-歌曲名称1,歌曲名称2艺人名称2歌曲名称1,歌曲名称2)这就是我认为使用for循环的原因。你能增强代码来做这件事吗requirement@JumRemdesk那一刻,pls@JumRemdesk好的,我更新我的答案,solusion是你想要的wokring