Extracts the title of the page based on the URL and automatically determines the Encoding Based on the charset of the page
Source: Internet
Author: User
Using System;
Using System. net;
Using System. text;
Using System. Text. regularexpressions;
Class Program
{
// Obtains the HTML content of a webpage and automatically determines the Encoding Based on the charset of the webpage.
Static String Gethtml ( String URL)
{
ReturnGethtml (URL,Null);
}
// Obtain the HTML content of the webpage and specify Encoding
Static String Gethtml ( String URL, encoding)
{
Byte [] Buf = New WebClient (). downloaddata (URL );
If (Encoding ! = Null ) Return Encoding. getstring (BUF );
String Html = Encoding. utf8.getstring (BUF );
Encoding = Getencoding (HTML );
If (Encoding = Null | Encoding = Encoding. utf8) Return HTML;
Return Encoding. getstring (BUF );
}
// Extract the webpage's encoding according to the webpage's HTML content
Static Encoding getencoding ( String Html)
{
String Pattern = @" (? I) \ bcharset = (? <Charset> [-a-zA-Z_0-9] +) " ;
String Charset = RegEx. Match (HTML, pattern). Groups [ " Charset " ]. Value;
Try {ReturnEncoding. getencoding (charset );}
Catch (Argumentexception) {Return Null;}
}
// Extract the title of a webpage based on the HTML content of the webpage
Static String Gettitle ( String Html)
{
StringPattern= @"(? Si) <title (? : \ S + (? : "" [^ ""] * "" | '[^'] * '| [^ ""'>]) *)?> (? <Title> .*?) </Title>";
ReturnRegEx. Match (HTML, pattern). Groups ["Title"]. Value. Trim ();
}
// Print the encoding and title of the webpage
Static Void Printencodingandtitle ( String URL)
{
StringHtml=Gethtml (URL );
Console. writeline ("[{0}] [{1}]", Getencoding (HTML), gettitle (HTML ));
}
// ProgramPortal
Static Void Main ()
{
Printencodingandtitle ( " Http://www.msdn.net/ " );
Printencodingandtitle ( " Http://www.cnblogs.com/ " );
Printencodingandtitle ( " Http://www.cnblogs.com/skyiv/ " );
Printencodingandtitle ( " Http://www.csdn.net/ " );
Printencodingandtitle ( " Http://news.163.com/ " );
}
}
/**/ /*Program output:
[] [Msdn: Microsoft Developer Network]
[System. Text. utf8encoding] [blog garden-programmer's online home]
[System. Text. utf8encoding] [Space/IV-blog park]
[System. Text. utf8encoding] [csdn. Net-the largest IT technology community in China, providing the most comprehensive information dissemination and service platform for IT professionals]
[System. Text. dbcscodepageencoding] [News center_netease news]
*/
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.