구문 분석 할 html이 있습니다 (아래 참조).
<div id="mailbox" class="div-w div-m-0">
<h2 class="h-line">InBox</h2>
<div id="mailbox-table">
<table id="maillist">
<tr>
<th>From</th>
<th>Subject</th>
<th>Date</th>
</tr>
<tr onclick="location='readmail.html?mid=welcome'" style="font-weight: bold;">
<td>no-reply@somemail.net</td>
<td>
<a href="readmail.html?mid=welcome">Hi, Welcome</a>
</td>
<td>
<span title="2016-02-16 13:23:50 UTC">just now</span>
</td>
</tr>
<tr onclick="location='readmail.html?mid=T0wM6P'" style="font-weight: bold;">
<td>someone@outlook.com</td>
<td>
<a href="readmail.html?mid=T0wM6P">sa</a>
</td>
<td>
<span title="2016-02-16 13:24:04">just now</span>
</td>
</tr>
</table>
</div>
</div>
<tr onclick=
태그 및 <td>
태그의 전자 메일 주소에있는 링크를 구문 분석해야합니다.
지금까지 나는 html에서 이메일 / 링크의 첫 번째 출현을 얻으려고했다.
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(responseFromServer);
누군가가 어떻게 제대로 수행되었는지를 보여줄 수 있습니까? 기본적으로 내가 원하는 것은 태그에있는 HTML에서 모든 전자 메일 주소와 링크를 가져 오는 것입니다.
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//tr[@onclick]"))
{
HtmlAttribute att = link.Attributes["onclick"];
Console.WriteLine(att.Value);
}
편집 : 클래스 (목록) 쌍으로 구문 분석 된 값을 저장해야합니다. 이메일 (링크) 및 발신자 이메일.
public class ClassMailBox
{
public string From { get; set; }
public string LinkToMail { get; set; }
}
다음 코드를 작성할 수 있습니다.
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(responseFromServer);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//tr[@onclick]"))
{
HtmlAttribute att = link.Attributes["onclick"];
ClassMailBox classMailbox = new ClassMailBox() { LinkToMail = att.Value };
classMailBoxes.Add(classMailbox);
}
int currentPosition = 0;
foreach (HtmlNode tableDef in doc.DocumentNode.SelectNodes("//tr[@onclick]/td[1]"))
{
classMailBoxes[currentPosition].From = tableDef.InnerText;
currentPosition++;
}
이 코드를 단순하게 유지하기 위해 몇 가지 가정합니다.
이러한 조건이 적용되지 않으면이 코드가 작동하지 않고 예외 (IndexOutOfRangeExceptions)가 발생하거나 잘못된 전자 메일 주소의 링크와 일치 할 수 있습니다.