나는 단어의 XX 양 이상의 텍스트 블록을 추출하는 방법이 있습니다. 문제는 그 텍스트 안의 링크를 반환하지 않을 것이라는 점입니다.
나의 방법 :
public string getAllTextHTML(string _html)
{
string _allText = "";
try
{
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(_html);
document.DocumentNode.Descendants()
.Where(n => n.Name == "script" || n.Name == "style")
.ToList()
.ForEach(n => n.Remove());
RemoveComments(document.DocumentNode);
var root = document.DocumentNode;
var sb = new StringBuilder();
foreach (var node in root.DescendantNodesAndSelf())
{
if (!node.HasChildNodes)
{
string text = node.InnerHtml;
if (!string.IsNullOrEmpty(text))
{
int antalOrd = WordCounting.CountWords1(text);
if (antalOrd > 25)
{
text = System.Web.HttpUtility.HtmlDecode(text);
sb.AppendLine(text.Trim());
}
}
}
}
_allText = sb.ToString();
}
catch (Exception)
{
}
_allText = System.Web.HttpUtility.HtmlDecode(_allText);
return _allText;
}
어떻게하면 텍스트 내 링크를 얻을 수 있을까요?
나는 다음과 같은 라인이 문제가된다고 생각한다.
if (!node.HasChildNodes)
왜냐하면 link (anchor)가 htlm 태그이기 때문에 앵커 태그가있는 html 태그를 자식으로 제외합니다.
다음은 링크를 반환하는 간단한 예제입니다.
String html = "<p>asdf<a href='#'>Test</a>asdfasd</p>";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
String p = (from x in doc.DocumentNode.Descendants()
where x.Name == "p"
select x.InnerHtml).FirstOrDefault();