Asp.net web page capture content, asp.net web page capture

Source: Internet
Author: User

Asp.net web page capture content, asp.net web page capture

Web page capture code

Using System; using System. collections. generic; using System. linq; using System. web; // using System. net; using System. IO; using System. text. regularExpressions; using System. text; namespace WSYL. web. common {public static class GetSteamShipInfo {public static string GetWebSite (string steamshipname, int itype) {if (steamshipname = null | steamshipname. trim () = "") return null; // step1: get html from url string urlToCrawl = @ ""; // generate http request HttpWebRequest req = (HttpWebRequest) WebRequest. create (urlToCrawl); // use GET method to get url's html req. method = "GET"; // use request to get response HttpWebResponse resp = (HttpWebResponse) req. getResponse (); // August 12, year 15 18:14:45 need to increase the judgment of Web Page parsing timeout to prevent web page false dead // string htmlCharset = "UTF-8"; string htmlCharset = "UTF-8 "; // use songtaste's html's charset GB2312 to decode html // otherwise will return messy code Encoding htmlEncoding = Encoding. getEncoding (htmlCharset); StreamReader sr = new StreamReader (resp. getResponseStream (), htmlEncoding); // read out the returned html string respHtml = sr. readToEnd (); // The third method to obtain the content. // Match TitleMatch = Regex. match (rtbExtractedHtml. text. toString (), "<td width = \" 30% \ "> ([^ <] *) </td>", RegexOptions. ignoreCase | RegexOptions. multiline );
// Start and end content of the Code to be obtained
Match TitleMatch2 = Regex. match (respHtml. toString (), "<td align = \" left \ "bgcolor = \" # EEEEEE \ "> ([^ <] *) </td>", RegexOptions. ignoreCase | RegexOptions. multiline); // txbExtractedInfo. text = TitleMatch2.Groups [1]. value + "/" + TitleMatch2.Groups [2]. value; if (TitleMatch2.Groups [1]. value. length = 0 | TitleMatch2.Groups [1]. value = "") return respHtml = ""; if (itype = 0) {respHtml = TitleMatch2.Groups [1]. val Ue. toString () ;}if (itype = 1) {respHtml = StripHtml (TitleMatch2.NextMatch (). value. toString ();} if (itype = 2) {respHtml = TitleMatch2.Groups [1]. value + "/" + StripHtml (TitleMatch2.NextMatch (). value. toString () ;}return respHtml ;}/// <summary >/// some exceptions may cause the removal of html tags and spaces, so we recommend that you convert them twice in a row. In this way, Html tags are converted to spaces. Too many consecutive spaces will affect subsequent string operations /// </summary> /// <param name = "strHtml"> tag content </param> /// <returns> </returns> private static string StripHtml (string strHtml) {Regex objRegExp = new Regex ("<(. | \ n) +?> "); String strOutput = objRegExp. replace (strHtml, ""); strOutput = strOutput. replace ("<", "& lt;"); strOutput = strOutput. replace (">", "& gt;"); // convert all spaces into a space Regex r = new Regex (@ "\ s +"); strOutput = r. replace (strOutput, ""); return strOutput. trim ();}}}

 

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.