c# htmlagilitypack - 如何从网页中提取特定文本
c# htmlagilitypack - how to extract specific text from web page
我正在尝试从
中提取描述
http://www.wowhead.com/quest=35151/your-base-your-choice
并将其显示到 richTextBox 控件中。
输出应该是:
You have already constructed an impressive garrison in Frostfire. I
believe I should defer this next choice to you. One region of Gorgrond
is rich in resources. A lumber mill could help us make the most of
them. Another region harbors hardened gladiators. A sparring arena
would help persuade them to fight for our cause. Either path will
strengthen us as we seek to find and weaken the Iron Horde. Which do
you choose, Commander?
下面是我目前试过的代码。
HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
html.LoadHtml(new WebClient().DownloadString("http://www.wowhead.com/quest=35151"));
var root = html.DocumentNode;
var p = root.Descendants("h2")
.Where(n => n.GetAttributeValue("class", "")
.Equals("heading-size-3"))
.FirstOrDefault().NextSibling;
richTextBox1.Text = p.InnerText;
但我得到的只是:
You have already constructed an impressive garrison in Frostfire. I
believe I should defer this next choice to you.
我为我的英语道歉。
您需要遍历第一个 .heading-size-3
到下一个 header .heading-size-3
之间的所有兄弟姐妹
HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
html.LoadHtml(new WebClient().DownloadString("http://www.wowhead.com/quest=35151"));
var root = html.DocumentNode;
var descriptionHeader = root.Descendants("h2")
.Where(n => n.GetAttributeValue("class", "")
.Equals("heading-size-3"))
.FirstOrDefault();
var current = descriptionHeader.NextSibling;
var result = "";
while(current != null && !current.GetAttributeValue("class", "")
.Equals("heading-size-3"))
{
if (!string.IsNullOrEmpty(current.InnerText))
{
result += " "+current.InnerText;
}
current = current.NextSibling;
}
richTextBox1.Text = result;
最后,您将获得:
You have already constructed an impressive garrison in Frostfire. I believe I should defer this next choice to you.
One region of Gorgrond is rich in resources. A lumber mill could help us make the most of them.
Another region harbors hardened gladiators. A sparring arena would help persuade them to fight for our cause.
Either path will strengthen us as we seek to find and weaken the Iron Horde.
Which do you choose, Commander?
我正在尝试从
中提取描述
http://www.wowhead.com/quest=35151/your-base-your-choice
并将其显示到 richTextBox 控件中。
输出应该是:
You have already constructed an impressive garrison in Frostfire. I believe I should defer this next choice to you. One region of Gorgrond is rich in resources. A lumber mill could help us make the most of them. Another region harbors hardened gladiators. A sparring arena would help persuade them to fight for our cause. Either path will strengthen us as we seek to find and weaken the Iron Horde. Which do you choose, Commander?
下面是我目前试过的代码。
HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
html.LoadHtml(new WebClient().DownloadString("http://www.wowhead.com/quest=35151"));
var root = html.DocumentNode;
var p = root.Descendants("h2")
.Where(n => n.GetAttributeValue("class", "")
.Equals("heading-size-3"))
.FirstOrDefault().NextSibling;
richTextBox1.Text = p.InnerText;
但我得到的只是:
You have already constructed an impressive garrison in Frostfire. I believe I should defer this next choice to you.
我为我的英语道歉。
您需要遍历第一个 .heading-size-3
到下一个 header .heading-size-3
HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
html.LoadHtml(new WebClient().DownloadString("http://www.wowhead.com/quest=35151"));
var root = html.DocumentNode;
var descriptionHeader = root.Descendants("h2")
.Where(n => n.GetAttributeValue("class", "")
.Equals("heading-size-3"))
.FirstOrDefault();
var current = descriptionHeader.NextSibling;
var result = "";
while(current != null && !current.GetAttributeValue("class", "")
.Equals("heading-size-3"))
{
if (!string.IsNullOrEmpty(current.InnerText))
{
result += " "+current.InnerText;
}
current = current.NextSibling;
}
richTextBox1.Text = result;
最后,您将获得:
You have already constructed an impressive garrison in Frostfire. I believe I should defer this next choice to you. One region of Gorgrond is rich in resources. A lumber mill could help us make the most of them. Another region harbors hardened gladiators. A sparring arena would help persuade them to fight for our cause. Either path will strengthen us as we seek to find and weaken the Iron Horde. Which do you choose, Commander?