검색결과 없음 HTML 추출 개선 및 도서관별 지연시간 저장 기능 구현
- 모든 도서관 검색기에서 검색결과 없음시 구체적인 HTML 조각 추출 - BookSearchResult에 Resulthtml 속성 추가하여 매칭된 HTML 컨텍스트 저장 - Helper_LibraryDelaySettings.cs 추가로 도서관별 검색 지연시간 XML 저장 - Check_copyWD.cs에 dvc_resulthtml 컬럼 표시 및 지연시간 저장 UI 구현 - 15개 SearchModel 파일에서 htmlContent 1000자 자르기를 의미있는 메시지로 교체 - HTTP 검색기들에 한글 인코딩 문제 해결을 위한 헤더 개선 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -269,7 +269,8 @@ namespace BokBonCheck
|
||||
await WaitForPageChange(new WebDriverWait(_driver, TimeSpan.FromSeconds(15)));
|
||||
|
||||
// 검색 결과 수 추출
|
||||
var resultCount = ExtractBookCount(_driver, searchTerm, out string ermsg);
|
||||
var resultCount = ExtractBookCount(_driver, searchTerm, out string ermsg, out string resultHtml);
|
||||
result.Resulthtml = resultHtml;
|
||||
if (resultCount == -1)
|
||||
{
|
||||
result.BookCount = 0;
|
||||
@@ -294,11 +295,14 @@ namespace BokBonCheck
|
||||
return result;
|
||||
}
|
||||
|
||||
private int ExtractBookCount(IWebDriver driver, string searchTerm, out string errmessage)
|
||||
private int ExtractBookCount(IWebDriver driver, string searchTerm, out string errmessage, out string resulthtml)
|
||||
{
|
||||
errmessage = string.Empty;
|
||||
resulthtml = string.Empty;
|
||||
try
|
||||
{
|
||||
var htmlContent = driver.PageSource;
|
||||
|
||||
// div.search-result 내부의 span에서 '전체 N' 텍스트 추출
|
||||
var resultDiv = driver.FindElement(By.CssSelector("div.search-result"));
|
||||
|
||||
@@ -306,6 +310,7 @@ namespace BokBonCheck
|
||||
if (bodytext.Contains("검색결과가 없습니다"))
|
||||
{
|
||||
errmessage = "검색결과없음";
|
||||
resulthtml = bodytext;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -315,6 +320,7 @@ namespace BokBonCheck
|
||||
if (searchTerm.Contains(searchtitle) == false)
|
||||
{
|
||||
errmessage = $"검색어불일치({searchtitle}/{searchTerm})";
|
||||
resulthtml = searchtitle;
|
||||
return -1;
|
||||
}
|
||||
var span = resultDiv.FindElement(By.XPath(".//span[contains(text(),'전체')]"));
|
||||
@@ -325,10 +331,13 @@ namespace BokBonCheck
|
||||
if (int.TryParse(match.Groups[1].Value, out int vqty) == false)
|
||||
{
|
||||
errmessage = $"수량값오류({match.Groups[1].Value})";
|
||||
resulthtml = match.Value;
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
errmessage = $"검색성공({vqty}건)";
|
||||
resulthtml = ExtractResultContext(htmlContent, match);
|
||||
searchTerm = string.Empty;
|
||||
return vqty;
|
||||
}
|
||||
@@ -336,6 +345,23 @@ namespace BokBonCheck
|
||||
else
|
||||
{
|
||||
errmessage = "수량항목없음";
|
||||
// 매칭된 부분이 없는 경우, 기본적으로 text 부분 추출 시도
|
||||
try
|
||||
{
|
||||
var dummyMatch = System.Text.RegularExpressions.Regex.Match(text, @"전체.*");
|
||||
if (dummyMatch.Success)
|
||||
{
|
||||
resulthtml = ExtractResultContext(htmlContent, dummyMatch);
|
||||
}
|
||||
else
|
||||
{
|
||||
resulthtml = htmlContent.Length > 500 ? htmlContent.Substring(0, 500) : htmlContent;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
resulthtml = htmlContent.Length > 500 ? htmlContent.Substring(0, 500) : htmlContent;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -343,11 +369,72 @@ namespace BokBonCheck
|
||||
catch (Exception ex)
|
||||
{
|
||||
errmessage = ex.Message;
|
||||
resulthtml = "ExtractBookCount 오류: " + ex.Message;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 매칭된 결과와 그 상위 태그를 추출
|
||||
/// </summary>
|
||||
private string ExtractResultContext(string htmlContent, Match match)
|
||||
{
|
||||
try
|
||||
{
|
||||
var matchIndex = match.Index;
|
||||
var matchLength = match.Length;
|
||||
|
||||
// 매칭된 위치 앞쪽에서 상위 태그 시작 찾기
|
||||
var startSearchIndex = Math.Max(0, matchIndex - 200); // 매칭 위치 200자 전부터 검색
|
||||
var searchText = htmlContent.Substring(startSearchIndex, matchIndex - startSearchIndex + matchLength + Math.Min(200, htmlContent.Length - matchIndex - matchLength));
|
||||
|
||||
// 상위 태그 패턴들 (div, p, h1-h6, span 등)
|
||||
var tagPatterns = new[] { @"<(div|p|h[1-6]|span|section|article)[^>]*>", @"<[^>]+>" };
|
||||
|
||||
string resultContext = match.Value; // 기본값은 매칭된 부분만
|
||||
|
||||
foreach (var tagPattern in tagPatterns)
|
||||
{
|
||||
// 매칭된 부분 앞에서 가장 가까운 태그 시작 찾기
|
||||
var tagMatches = Regex.Matches(searchText, tagPattern, RegexOptions.IgnoreCase);
|
||||
|
||||
for (int i = tagMatches.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var tagMatch = tagMatches[i];
|
||||
if (tagMatch.Index < (matchIndex - startSearchIndex))
|
||||
{
|
||||
// 태그 이름 추출
|
||||
var tagName = Regex.Match(tagMatch.Value, @"<(\w+)", RegexOptions.IgnoreCase).Groups[1].Value;
|
||||
|
||||
// 닫는 태그 찾기
|
||||
var closeTagPattern = $@"</{tagName}[^>]*>";
|
||||
var closeMatch = Regex.Match(searchText, closeTagPattern, RegexOptions.IgnoreCase);
|
||||
|
||||
if (closeMatch.Success && closeMatch.Index > (matchIndex - startSearchIndex))
|
||||
{
|
||||
// 상위 태그와 그 내용을 포함하여 반환
|
||||
var startIdx = tagMatch.Index;
|
||||
var endIdx = closeMatch.Index + closeMatch.Length;
|
||||
resultContext = searchText.Substring(startIdx, Math.Min(endIdx - startIdx, 500)); // 최대 500자
|
||||
return resultContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 상위 태그를 찾지 못한 경우, 매칭 전후 50자씩 포함
|
||||
var contextStart = Math.Max(0, matchIndex - 50);
|
||||
var contextEnd = Math.Min(htmlContent.Length, matchIndex + matchLength + 50);
|
||||
resultContext = htmlContent.Substring(contextStart, contextEnd - contextStart);
|
||||
|
||||
return resultContext;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"ExtractResultContext 오류: {ex.Message}");
|
||||
return match.Value; // 오류 시 매칭된 부분만 반환
|
||||
}
|
||||
}
|
||||
|
||||
// 페이지 변경을 감지하는 메서드
|
||||
public async Task WaitForPageChange(WebDriverWait wait)
|
||||
|
||||
Reference in New Issue
Block a user