ASP. NET collection example: a small Web Crawler

Source: Internet
Author: User


[Original] ASP. NET collection examples Baidu search K8 robots capture titles and links change URLs and regular expressions can randomly capture specified sites
Or add a multi-thread program and related vulnerability tests to write a program that automatically invades the website. The following is a simple example.
Using System;
Using System. Collections. Generic;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Text. RegularExpressions;
Using System. Net;
Using System. Text;

Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{

}
Protected void button#click (object sender, EventArgs e)
{
String htmlcode = GetHTML ("http://www.baidu.com/s? Wd = K8 % BB % FA % C6 % F7 % C8 % CB "); // K8 Robot

Regex reg = new Regex ("<table cellpadding = \" 0 \ ". * href = \"(? <K8url>. +). * target = \ "_ blank \"> (? <K8title>. + )");

MatchCollection MS = reg. Matches (htmlcode );
Foreach (Match m in MS)
{
// This. Response. Write (m. Groups ["k8url"] + "" + m. Groups ["k8title"] + "<br> ");
This. response. write ("<a href = \" "+ m. groups ["k8url"] + "\"> "+ m. groups ["k8title"] + "</a> <br> ");
}
}

Public string GetHTML (string url)
{
WebClient web = new WebClient ();
Byte [] buffer = web. DownloadData (url );
Return Encoding. Default. GetString (buffer );
}
}

Related Article

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.