これは、ASP.Net(C#)のHtml Agility Packで解析しようとしているサンプルHTMLです。
<div class="content-div">
<dl>
<dt>
<b><a href="1.html" title="1">1</a></b>
</dt>
<dd> First Entry</dd>
<dt>
<b><a href="2.html" title="2">2</a></b>
</dt>
<dd> Second Entry</dd>
<dt>
<b><a href="3.html" title="3">3</a></b>
</dt>
<dd> Third Entry</dd>
</dl>
</div>
私が求めている価値は次のとおりです:
(ここでは最初のエントリの例を取り上げましたが、リスト内のすべてのエントリに対してこれらの要素の値が必要です)
これは現在私が使用しているコードですが、
var webGet = new HtmlWeb();
var document = webGet.Load(url2);
var parsedValues=
from info in document.DocumentNode.SelectNodes("//div[@class='content-div']")
from content in info.SelectNodes("dl//dd")
from link in info.SelectNodes("dl//dt/b/a")
.Where(x => x.Attributes.Contains("href"))
select new
{
Text = content.InnerText,
Url = link.Attributes["href"].Value,
AnchorText = link.InnerText,
};
GridView1.DataSource = parsedValues;
GridView1.DataBind();
問題は、リンクとアンカーテキストの値を正しく取得することですが、内部テキストの場合、最初のエントリの値をとり、要素が発生した合計回数に対して他のすべてのエントリに対して同じ値を入力します。それは2番目のものから始まります。私の説明ではあまり明確ではないかもしれませんので、ここではこのコードを使ったサンプル出力を示します:
First Entry 1.html 1
First Entry 2.html 2
First Entry 3.html 3
Second Entry 1.html 1
Second Entry 2.html 2
Second Entry 3.html 3
Third Entry 1.html 1
Third Entry 2.html 2
Third Entry 3.html 3
私が取得しようとしているのに対して
First Entry 1.html 1
Second Entry 2.html 2
Third Entry 3.html 3
私はHAPにはかなり新しいですし、xpathにはほとんど知られていないので、私はここで何か間違っていると確信していますが、時間を費やしてもそれを動作させることはできませんでした。どんな助けでも大歓迎です。
解決策1
私は、 dt
ノードが与えられた後に次のdd
ノードを返す関数を定義しました:
private static HtmlNode GetNextDDSibling(HtmlNode dtElement)
{
var currentNode = dtElement;
while (currentNode != null)
{
currentNode = currentNode.NextSibling;
if(currentNode.NodeType == HtmlNodeType.Element && currentNode.Name =="dd")
return currentNode;
}
return null;
}
LINQコードは次のように変換できます。
var parsedValues =
from info in document.DocumentNode.SelectNodes("//div[@class='content-div']")
from dtElement in info.SelectNodes("dl/dt")
let link = dtElement.SelectSingleNode("b/a[@href]")
let ddElement = GetNextDDSibling(dtElement)
where link != null && ddElement != null
select new
{
Text = ddElement.InnerHtml,
Url = link.GetAttributeValue("href", ""),
AnchorText = link.InnerText
};
解決策2
追加機能なし:
var infoNode =
document.DocumentNode.SelectSingleNode("//div[@class='content-div']");
var dts = infoNode.SelectNodes("dl/dt");
var dds = infoNode.SelectNodes("dl/dd");
var parsedValues = dts.Zip(dds,
(dt, dd) => new
{
Text = dd.InnerHtml,
Url = dt.SelectSingleNode("b/a[@href]").GetAttributeValue("href", ""),
AnchorText = dt.SelectSingleNode("b/a[@href]").InnerText
});
たとえば、 Html Agility Pack
を使用していくつかの要素を解析する方法の例
public string ParseHtml()
{
string output = null;
HtmlDocument htmldocument = new HtmlDocument();
htmldocument.LoadHtml(YourHTML);
HtmlNode node = htmldocument.DocumentNode;
HtmlNodeCollection dds = node.SelectNodes("//dd"); //Select all dd tags
HtmlNodeCollection anchors = node.SelectNodes("//b/a[@href]"); //Select all 'a' tags that contais href attribute
for (int i = 0; i < dds.Count; i++)
{
string atributteValue = null.
Text = dds[i].InnerText;
Url = anchors[i].GetAttributeValue("href", atributteValue);
AnchorText = anchors[i].InnerText;
//Your code...
}
return output;
}