웹 크롤러에서 작업하고 있습니다. 그 순간 나는 전체 내용을 긁은 다음 정규 표현식을 사용하여 <meta>, <script>, <style>
및 다른 태그를 제거하고 본문의 내용을 가져옵니다.<body> 태그 만 웹 사이트에서 긁어내는 방법
그러나 성능을 최적화하기 위해 노력하고 있으며 페이지의 <body>
을 다 긁을 수있는 방법이 있는지 궁금합니다. Page_Load
에서
namespace WebScrapper
{
public static class KrioScraper
{
public static string scrapeIt(string siteToScrape)
{
string HTML = getHTML(siteToScrape);
string text = stripCode(HTML);
return text;
}
public static string getHTML(string siteToScrape)
{
string response = "";
HttpWebResponse objResponse;
HttpWebRequest objRequest =
(HttpWebRequest) WebRequest.Create(siteToScrape);
objRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; " +
"Windows NT 5.1; .NET CLR 1.0.3705)";
objResponse = (HttpWebResponse) objRequest.GetResponse();
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()))
{
response = sr.ReadToEnd();
sr.Close();
}
return response;
}
public static string stripCode(string the_html)
{
// Remove google analytics code and other JS
the_html = Regex.Replace(the_html, "<script.*?</script>", "",
RegexOptions.Singleline | RegexOptions.IgnoreCase);
// Remove inline stylesheets
the_html = Regex.Replace(the_html, "<style.*?</style>", "",
RegexOptions.Singleline | RegexOptions.IgnoreCase);
// Remove HTML tags
the_html = Regex.Replace(the_html, "</?[a-z][a-z0-9]*[^<>]*>", "");
// Remove HTML comments
the_html = Regex.Replace(the_html, "<!--(.|\\s)*?-->", "");
// Remove Doctype
the_html = Regex.Replace(the_html, "<!(.|\\s)*?>", "");
// Remove excessive whitespace
the_html = Regex.Replace(the_html, "[\t\r\n]", " ");
return the_html;
}
}
}
나는 그것을 내가하는 페이지에서 텍스트 상자에서 얻을 문자열을 통과하는 scrapeIt()
메서드를 호출합니다.
물론, 현재 스크래핑 코드를보아야합니다. –