Cet article est un article miroir de traduction automatique, veuillez cliquer ici pour accéder à l’article original.

Vue: 22625|Répondre: 0

[Source] C# Extraire et exporter toutes les méthodes de liens hypertexte sur la page web

[Copié le lien]
Publié sur 15/07/2015 08:30:08 | | |
  1. public class app
  2.     {
  3.         // 获取指定网页的HTML代码
  4.         public static string GetPageSource(string URL)
  5.         {
  6.             Uri uri = new Uri(URL);

  7.             HttpWebRequest hwReq = (HttpWebRequest)WebRequest.Create(uri);
  8.             HttpWebResponse hwRes = (HttpWebResponse)hwReq.GetResponse();

  9.             hwReq.Method = "Get";

  10.             hwReq.KeepAlive = false;

  11.             StreamReader reader = new StreamReader(hwRes.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));

  12.             return reader.ReadToEnd();
  13.         }
  14.         // 提取HTML代码中的网址
  15.         public static ArrayList GetHyperLinks(string htmlCode)
  16.         {
  17.             ArrayList al = new ArrayList();

  18.             string strRegex = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";

  19.             Regex r = new Regex(strRegex, RegexOptions.IgnoreCase);
  20.             MatchCollection m = r.Matches(htmlCode);

  21.             for (int i = 0; i <= m.Count - 1; i++)
  22.             {
  23.                 bool rep = false;
  24.                 string strNew = m[i].ToString();

  25.                 // 过滤重复的URL
  26.                 foreach (string str in al)
  27.                 {
  28.                     if (strNew == str)
  29.                     {
  30.                         rep = true;
  31.                         break;
  32.                     }
  33.                 }

  34.                 if (!rep) al.Add(strNew);
  35.             }

  36.             al.Sort();

  37.             return al;
  38.         }
  39.         // 把网址写入xml文件
  40.         public static void WriteToXml(string strURL, ArrayList alHyperLinks)
  41.         {
  42.             XmlTextWriter writer = new XmlTextWriter("HyperLinks.xml", Encoding.UTF8);

  43.             writer.Formatting = Formatting.Indented;
  44.             writer.WriteStartDocument(false);
  45.             writer.WriteDocType("HyperLinks", null, "urls.dtd", null);
  46.             writer.WriteComment("提取自" + strURL + "的超链接");
  47.             writer.WriteStartElement("HyperLinks");
  48.             writer.WriteStartElement("HyperLinks", null);
  49.             writer.WriteAttributeString("DateTime", DateTime.Now.ToString());


  50.             foreach (string str in alHyperLinks)
  51.             {
  52.                 string title = GetDomain(str);
  53.                 string body = str;
  54.                 writer.WriteElementString(title, null, body);
  55.             }

  56.             writer.WriteEndElement();
  57.             writer.WriteEndElement();

  58.             writer.Flush();
  59.             writer.Close();
  60.         }

  61.         // 获取网址的域名后缀
  62.         static string GetDomain(string strURL)
  63.         {
  64.             string retVal;

  65.             string strRegex = @"(\.com/|\.net/|\.cn/|\.org/|\.gov/)";

  66.             Regex r = new Regex(strRegex, RegexOptions.IgnoreCase);
  67.             Match m = r.Match(strURL);
  68.             retVal = m.ToString();

  69.             strRegex = @"\.|/$";
  70.             retVal = Regex.Replace(retVal, strRegex, "").ToString();

  71.             if (retVal == "")
  72.                 retVal = "other";

  73.             return retVal;
  74.         }



  75.     }
Code de copie
  1. private void btnkaishi_Click(object sender, EventArgs e)
  2.         {
  3.             string strCode;
  4.             ArrayList alLinks;


  5.             if (txtapi.Text == "")
  6.             {
  7.                 MessageBox.Show("请输入网址");
  8.                 return;
  9.             }
  10.             string strURL = txtapi.Text.ToString().Trim();
  11.             if (strURL.Substring(0, 7) != @"http://")
  12.             {
  13.                 strURL = @"http://" + strURL;
  14.             }
  15.             MessageBox.Show("正在获取页面代码,请稍后...");
  16.             strCode = app.GetPageSource(strURL);
  17.             MessageBox.Show("正在提取超链接,请稍侯...");
  18.             alLinks = app.GetHyperLinks(strCode);
  19.             MessageBox.Show("正在写入文件,请稍侯...");
  20.             app.WriteToXml(strURL, alLinks);

  21.         }
Code de copie






Précédent:C# exporte le caractère de chaîne vers la méthode du document txt
Prochain:[Original] Baidu Active Push Tool v2.0 Téléchargement
Démenti:
Tous les logiciels, supports de programmation ou articles publiés par Code Farmer Network sont uniquement destinés à l’apprentissage et à la recherche ; Le contenu ci-dessus ne doit pas être utilisé à des fins commerciales ou illégales, sinon les utilisateurs assumeront toutes les conséquences. Les informations sur ce site proviennent d’Internet, et les litiges de droits d’auteur n’ont rien à voir avec ce site. Vous devez supprimer complètement le contenu ci-dessus de votre ordinateur dans les 24 heures suivant le téléchargement. Si vous aimez le programme, merci de soutenir un logiciel authentique, d’acheter l’immatriculation et d’obtenir de meilleurs services authentiques. En cas d’infraction, veuillez nous contacter par e-mail.

Mail To:help@itsvse.com