일부 데이터를 추출하려면 웹 사이트에서 Html Agility Pack을 사용하고 있습니다. 필요한 HTML을 파싱하는 것은 쉽지만이 (다소 복잡합니까?) HTML 조각에 문제가 있습니다.
<tr>
<td>
<div onmouseover="toggle('clue_J_1_1', 'clue_J_1_1_stuck', '<em class="correct_response">Obama</em><br /><br /><table width="100%"><tr><td class="right">Kailyn</td></tr></table>')" onmouseout="toggle('clue_J_1_1', 'clue_J_1_1_stuck', 'Michelle LaVaughn Robinson')" onclick="togglestick('clue_J_1_1_stuck')">
...
나는 clue_J_X_Y 값을 기반으로 onmouseover div의 em 클래스 "correct_response"에서 값을 가져와야합니다. 나는 이걸 넘어서는 방법을 모른다.
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//tr//td/div[@onmouseover]");
어떤 도움을 주시면 감사하겠습니다.
너에게서 뭘 빠져 나가야할지 모르겠다. 하지만 당신이 알아야 할 모든 데이터를 알려 드리겠습니다.
먼저 HTML을로드합니다.
string html = "<tr>" +
"<td>" +
"<div onmouseover = \"toggle('clue_J_1_1', 'clue_J_1_1_stuck', '<em class="correct_response">Obama</em><br/><br/><table width="100%"><tr><td class="right">Kailyn</td></tr></table>')\" onmouseout = \"toggle('clue_J_1_1', 'clue_J_1_1_stuck', 'Michelle LaVaughn Robinson')\" onclick = \"togglestick('clue_J_1_1_stuck')\"></div></td></tr>";
HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
//Console.WriteLine(doc.DocumentNode.OuterHtml);
그런 다음 속성 onmouseover의 값을 얻습니다.
string toggle = doc.DocumentNode.SelectSingleNode("//tr//td/div[@onmouseover]").GetAttributeValue("onmouseover", "FAILED");
"onmouseover"라는 속성을 찾지 못하면 FAILED를 리턴합니다. 이제 각각이 2로 둘러싸인 토글 방법의 매개 변수를 얻습니다 (아포스트로피).
//Get Variables from toggle()
List<string> toggleVariables = new List<string>();
bool flag = false; string temp = "";
for(int i=0; i<toggle.Length; i++)
{
if (toggle[i] == '\'' && flag== true)
{
toggleVariables.Add(temp);
temp = "";
flag = false;
}
else if (flag)
{
temp += toggle[i];
}
else if (toggle[i] == '\'')
{
flag = true;
}
}
그 다음 엔티티가 3 개있는 목록이 있습니다. 이 경우 다음이 포함됩니다.
이제 세 번째 매개 변수의 HTML 코드로 새 HtmlDocument를 만들 수 있습니다. 하지만 세 번째 매개 변수는 HTML의 이스케이프 문자를 포함하기 때문에 먼저이를 실행 가능한 HTML로 변환해야합니다.
//Make it into workable HTML
toggleVariables[2] = HttpUtility.HtmlDecode(toggleVariables[2]);
//New HtmlDocument
HtmlDocument htmlInsideToggle = new HtmlDocument();
htmlInsideToggle.LoadHtml(toggleVariables[2]);
Console.WriteLine(htmlInsideToggle.DocumentNode.OuterHtml);
그리고 끝났어. 전체 코드는 여기에서 아래에 있습니다.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using HtmlAgilityPack;
using System.Web;
namespace test
{
class Program
{
public static void Main(string[] args)
{
string html = "<tr>" +
"<td>" +
"<div onmouseover = \"toggle('clue_J_1_1', 'clue_J_1_1_stuck', '<em class="correct_response">Obama</em><br/><br/><table width="100%"><tr><td class="right">Kailyn</td></tr></table>')\" onmouseout = \"toggle('clue_J_1_1', 'clue_J_1_1_stuck', 'Michelle LaVaughn Robinson')\" onclick = \"togglestick('clue_J_1_1_stuck')\"></div></td></tr>";
HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
//Console.WriteLine(doc.DocumentNode.OuterHtml);
string toggle = doc.DocumentNode.SelectSingleNode("//tr//td/div[@onmouseover]").GetAttributeValue("onmouseover", "FAILED");
//Clean up string
//Console.WriteLine(toggle);
//Get Variables from toggle()
List<string> toggleVariables = new List<string>();
bool flag = false; string temp = "";
for(int i=0; i<toggle.Length; i++)
{
if (toggle[i] == '\'' && flag== true)
{
toggleVariables.Add(temp);
temp = "";
flag = false;
}
else if (flag)
{
temp += toggle[i];
}
else if (toggle[i] == '\'')
{
flag = true;
}
}
//Make it into workable HTML
toggleVariables[2] = HttpUtility.HtmlDecode(toggleVariables[2]);
//New HtmlDocument
HtmlDocument htmlInsideToggle = new HtmlDocument();
htmlInsideToggle.LoadHtml(toggleVariables[2]);
Console.WriteLine(htmlInsideToggle.DocumentNode.OuterHtml);
//You're on your own from here
Console.ReadKey();
}
}