나는 다음과 같이 문서를 파싱해야한다. HtmlAgilityPack을 시도하고 있지만 매우 복잡합니다. 이 태그 내부 텍스트가 필요합니다. <td style="background: #36461f;color: #ffffff;font-weight: bold;padding: 2px;font-size: 12px;height: 25px;">Mac Bahsi</td>
아이들
<div class="div1" style="width: 288px;" onclick="parent.javaScriptAddSlip('slip', '164518117;;;-;11.25;1;Maç Bahsi;164518117')">
<div class="div1" style="width: 288px;" onclick="parent.javaScriptAddSlip('slip', '164518117;;;-;6.50;0;Maç Bahsi;164518117')">,
<div class="div1" style="width: 288px;" onclick="parent.javaScriptAddSlip('slip', '164518117;;;-;1.18;2;Maç Bahsi;164518117')">
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
.table1 {
width: 100%;
margin: 0px;
padding: 0px;
border-collapse: collapse;
padding: 0px;
}
.div1 {
cursor: pointer;
margin: 1px;
border: 1px solid #999999;
float: left;
font-size: 12px;
}
.td1 {
text-align: center;
font-size: 20px;
font-weight: bold;
color: #33460E;
height: 20px;
padding: 0px;
}
.td2 {
text-align: center;
font-weight: bold;
color: #808000;
padding: 0px;
}
</style>
</head>
<body style="background: #FFFFCC;margin: 0px;padding: 0px;font-size: 12px;">
<p></p>
<table style="width: 100%" cellpadding="0" cellspacing="0">
<tr>
<td style="background: #36461f;color: #ffffff;font-weight: bold;padding: 2px;font-size: 12px;height: 25px;">Mac Bahsi</td>
</tr>
<tr>
<td>
<div class="div1" style="width: 288px;" onclick="parent.javaScriptAddSlip('slip', '164518117;;;-;11.25;1;Maç Bahsi;164518117')">
<table class="table1">
<tr class="menuClickable">
<td class="td1">11.25</td>
</tr>
<tr class="menuClickable">
<td class="td2">Club America Mexico</td>
</tr>
</table>
</div>
<div class="div1" style="width: 288px;" onclick="parent.javaScriptAddSlip('slip', '164518117;;;-;6.50;0;Maç Bahsi;164518117')">
<table class="table1">
<tr class="menuClickable">
<td class="td1">6.50</td>
</tr>
<tr class="menuClickable">
<td class="td2">Beraberlik</td>
</tr>
</table>
</div>
<div class="div1" style="width: 288px;" onclick="parent.javaScriptAddSlip('slip', '164518117;;;-;1.18;2;Maç Bahsi;164518117')">
<table class="table1">
<tr class="menuClickable">
<td class="td1">1.18</td>
</tr>
<tr class="menuClickable">
<td class="td2">Real Madrid</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<table style="width: 100%" cellpadding="0" cellspacing="0">
<tr>
<td style="background: #36461f;color: #ffffff;font-weight: bold;padding: 2px;font-size: 12px;height: 25px;">Ilk Yari Bahsi</td>
</tr>
<tr>
<td>
<div class="div1" style="width: 288px;" onclick="parent.javaScriptAddSlip('slip', '164518128;;;-;8.50;1;İlk Yarı Bahsi;164518128')">
<table class="table1">
<tr class="menuClickable">
<td class="td1">8.50</td>
</tr>
<tr class="menuClickable">
<td class="td2">Club America Mexico</td>
</tr>
</table>
</div>
<div class="div1" style="width: 288px;" onclick="parent.javaScriptAddSlip('slip', '164518128;;;-;3.05;0;İlk Yarı Bahsi;164518128')">
<table class="table1">
<tr class="menuClickable">
<td class="td1">3.05</td>
</tr>
<tr class="menuClickable">
<td class="td2">Beraberlik</td>
</tr>
</table>
</div>
<div class="div1" style="width: 288px;" onclick="parent.javaScriptAddSlip('slip', '164518128;;;-;1.50;2;İlk Yarı Bahsi;164518128')">
<table class="table1">
<tr class="menuClickable">
<td class="td1">1.50</td>
</tr>
<tr class="menuClickable">
<td class="td2">Real Madrid</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<table style="width: 100%" cellpadding="0" cellspacing="0">
<tr>
<td style="background: #36461f;color: #ffffff;font-weight: bold;padding: 2px;font-size: 12px;height: 25px;">İkinci Yarı Bahsi</td>
</tr>
<tr>
<td>
<div class="div1" style="width: 288px;" onclick="parent.javaScriptAddSlip('slip', '164518133;;;-;8.50;1;İkinci Yarı Bahsi;164518133')">
<table class="table1">
<tr class="menuClickable">
<td class="td1">8.50</td>
</tr>
<tr class="menuClickable">
<td class="td2">Club America Mexico</td>
</tr>
</table>
</div>
<div class="div1" style="width: 288px;" onclick="parent.javaScriptAddSlip('slip', '164518133;;;-;3.70;0;İkinci Yarı Bahsi;164518133')">
<table class="table1">
<tr class="menuClickable">
<td class="td1">3.70</td>
</tr>
<tr class="menuClickable">
<td class="td2">Beraberlik</td>
</tr>
</table>
</div>
<div class="div1" style="width: 288px;" onclick="parent.javaScriptAddSlip('slip', '164518133;;;-;1.40;2;İkinci Yarı Bahsi;164518133')">
<table class="table1">
<tr class="menuClickable">
<td class="td1">1.40</td>
</tr>
<tr class="menuClickable">
<td class="td2">Real Madrid</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<br />
<br />
<br />
</body>
</html>
먼저 HTMLAgilityPack 너겟 패키지를 프로젝트에 설치하십시오.
그런 다음 예제로 :
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
// There are various options, set as needed
htmlDoc.OptionFixNestedTags=true;
// filePath is a path to a file containing the html
htmlDoc.Load(filePath);
// Use: htmlDoc.LoadHtml(xmlString); to load from a string (was htmlDoc.LoadXML(xmlString)
// ParseErrors is an ArrayList containing any errors from the Load statement
if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0)
{
// Handle any parse errors as required
}
else
{
if (htmlDoc.DocumentNode != null)
{
HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//body");
if (bodyNode != null)
{
// Do something with bodyNode
}
}
}
(주의 :이 코드는 단지 예일 뿐이며 반드시 최선의 방법은 아닙니다. 자신의 응용 프로그램에서 맹목적으로 사용하지 마십시오.)
HtmlDocument.Load()
메서드는 .NET Framework의 다른 스트림 지향 클래스와의 통합에 매우 유용한 스트림을 받아들입니다. HtmlEntity.DeEntitize()
가 html 엔티티를 올바르게 처리하는 데 유용한 또 다른 방법입니다. (Matthew에게 감사드립니다)
HtmlDocument
와 HtmlNode
는 가장 많이 사용할 클래스입니다. XML 구문 분석기와 마찬가지로 XPath 표현식을 허용하는 selectSingleNode 및 selectNodes 메서드를 제공합니다.
HtmlDocument.Option??????
주의를 기울이십시오. 부울 속성 이 메소드는 Load
및 LoadXML
메서드가 HTML / XHTML을 처리하는 방법을 제어합니다.
HtmlAgilityPack.chm이라는 컴파일 된 도움말 파일이 있으며 각 개체에 대한 완전한 참조가 있습니다. 이것은 일반적으로 솔루션의 기본 폴더에 있습니다.