用C#写的垂直搜索之爬虫

news/2024/7/19 10:11:34 标签: 爬虫
搞垂直搜索搞了这么写天,现在已经把我们学院的网页爬完了,非常快,时间不超过30s.






  1 using  System;
  2 using  System.Collections.Generic;
  3 using  System.ComponentModel;
  4 using  System.Data;
  5 using  System.Drawing;
  6 using  System.Text;
  7 using  System.Windows.Forms;
  8 using  System.Web.Security;
  9 using  System.IO;
 10 using  System.Net;
 11 using  System.Text.RegularExpressions;
 12
 13 namespace  ie
 14 {
 15    public partial class Form1 : Form
 16    {
 17        public Form1()
 18        {
 19            InitializeComponent();
 20
 21        }

 22
 23        private void button1_Click(object sender, EventArgs e)
 24        {
 25
 26            for (Int32 i = 100; i <= 1750; i++)
 27            {
 28                String ii = i.ToString();
 29
 30                string strmd5 = FormsAuthentication.HashPasswordForStoringInConfigFile(ii, "md5");
 31                string x = "http://ie.wh.sdu.edu.cn/show." + strmd5 + ".ie";
 32                readText read = new readText();
 33                read.setUrl(x);
 34                if (read.equalS() == 0continue;
 35                else
 36                {
 37                    read.writeText(ii);
 38                }

 39            }

 40            MessageBox.Show("完成!");
 41        }

 42        public class readText
 43        {
 44            private string[] text;
 45            private string url;
 46            public readText()
 47            {
 48                text = new string[6];
 49                for (int i = 0; i <= 5; i++)
 50                    text[i] = "";
 51                url = "";
 52            }

 53            public void setUrl(string urr)
 54            {
 55
 56                url = urr;
 57            }

 58
 59            public int equalS()
 60            {
 61                int x = read();
 62                if (x == -1return 0;
 63                for (int i = 0; i < 5; i++)
 64                {
 65                    string s = text[i];
 66                    if (s.Trim().Equals(""))
 67                        return 0;
 68                }

 69                return 1;
 70            }

 71
 72
 73            public void writeText(String str)
 74            {
 75
 76                string s = url + "\r\n";
 77                for (int i = 0; i <= 5; i++)
 78                {
 79                    s = s + text[i] + "\r\n";
 80                }

 81                string ss = "d:\\ie\\" + str + ".txt";
 82                if (!Directory.Exists("d:\\ie\\"))
 83                    Directory.CreateDirectory("d:\\ie\\");
 84                File.AppendAllText(ss, s);
 85            }

 86            public int read()
 87            {
 88
 89                string sss;
 90                WebRequest wreq = WebRequest.Create(url);//_url是字符串,代表要得到的网页。
 91                WebResponse wres = wreq.GetResponse();
 92                Stream stream = wres.GetResponseStream();//这里得到的流是网页内容
 93                if (stream == nullreturn -1;
 94                StreamReader sr = new StreamReader(stream, Encoding.Default);
 95                StringBuilder sb = new StringBuilder();
 96                string rl;
 97                while ((rl = sr.ReadLine()) != null)
 98                {
 99                    sb.Append(rl);
100                }

101                sss = sb.ToString();
102                wres.Close();
103                pReadIe(sss);
104                return 0;
105            }

106            public void pReadIe(String sInput)
107            {
108                MatchCollection matches;
109                Regex[] extractHTML = new Regex[5];
110                extractHTML[0= new Regex("<td class=\"title\" align=\"center\">(?<name>[^/]+)</td>");
111                extractHTML[1= new Regex(@"作者:[\s]+(?<name>[\S]+)");
112                extractHTML[2= new Regex("<td align=\"center\" class=\"adddatetime\">(?<data>[^ ]+)[\\s](?<time>[^ ]+)</td>");
113                extractHTML[3= new Regex(@"&nbsp;&nbsp;浏览:[\s]+(?<num>[\d]+)[\s]+次</td>");
114                extractHTML[4= new Regex("<td align=\"left\" class=\"content\">(?<count>[\\w\\W]+)</td>[\\s]+</tr>[\\s]+<tr>[\\s]+<td align=");
115                for (int i = 0; i <= 4; i++)
116                {
117                    matches = extractHTML[i].Matches(sInput);
118
119                    foreach (Match matchMade in matches)
120                    {
121
122                        switch (i)
123                        {
124                            case 0: text[0= matchMade.Groups[1].Value; break;
125                            case 1: text[1= matchMade.Groups[1].Value; break;
126                            case 2: text[2= matchMade.Groups[1].Value;
127                                text[3= matchMade.Groups[2].Value;
128                                break;
129                            case 3: text[4= matchMade.Groups[1].Value; break;
130                            case 4: text[5= matchMade.Groups[1].Value; break;
131                        }

132
133                    }

134                }

135            }

136        }

137
138
139    }

140}

141
142  

转载于:https://www.cnblogs.com/DonePuzzle/archive/2007/10/27/939486.html


http://www.niftyadmin.cn/n/1487418.html

相关文章

JAVA通信系列一:Java Socket技术总结

本文是学习java Socket整理的资料&#xff0c;供参考。 1 Socket通信原理 1.1 ISO七层模型 1.2 TCP/IP五层模型 应用层相当于OSI中的会话层&#xff0c;表示层&#xff0c;应用层。 区别参考&#xff1a;http://blog.chinaunix.net/uid-22166872-id-3716751.html…

Collections.sort与Arrrays.sort

文章目录概述Collections.sortList.sort总结概述 来看看Collections.sort与Arrrays.sort的排序算法&#xff0c;本文基于JDK1.8。 Collections.sort 容器工具类Collections.sort提供了以下几种方式&#xff1a; public static <T extends Comparable<? super T>&…

应该成为一名系统工程师么?

题外话从家长到老师、从学生到职场精英&#xff0c;每个人都在说中国是应试教育&#xff0c;不是素质教育&#xff1b;中国是发展中国家&#xff0c;没有职业市场&#xff0c;一个人能成就什么&#xff0c;靠的是运气、胆量而不是技能、创造力。是这样么&#xff1f;历史有惊人…

以opencv320和vs2013为例说明 vs路径配置方法

新方法 新方法的配置过程&#xff0c;路径之类的设置都是一样。 区别在于&#xff0c;在一开始&#xff0c;在属性管理器中新建一个opencv专属的属性页 之后所有的配置&#xff0c;都在这个属性也上进行 保存这个属性页。 如后如果有新的工程建立&#xff0c;把这个属性添加进…

Unity3d--Space Shooter(官方教程)--学习感想(3)

游戏环境已经基本搭建完成。 接下来需要用脚本实现飞船的移动、射击、爆炸等效果。 脚本语言采用C# 并在Project视图中创建一个专门存放脚本的目录。 选择Assets。Create->Folder创建一个新文件夹&#xff0c;更名为Scripts。 3.1 给飞船添加移动功能 点击Player&#xff0…

如何从数据库中筛选出达成指定里程碑节点的项目_客户实践 | Jira 在大型测试管理团队中的测试管理解决方案 | IDCF...

内容来源于&#xff1a;Atlassian速递 作者&#xff1a;杨刚杨刚&#xff0c;烽火通信科技股份有限公司信息化中心测试电子信息平台项目经理&#xff0c;负责烽火通信测试端到端管理平台建设&#xff0c;在系统集成平台开发方面有多年丰富的管理和实施经验。本文从研发测试核心…

Oracle Sys用户用默认密码change_on_install 无法登录的问题(错误代码:ORA-28009)

在使用默认Oracle自带的SQL Plus登录数据库时&#xff08;如下图&#xff09;&#xff0c;使用system用户默认密码manager登录没有问题。但是同样的输入用户sys和默认密码change_on_install确无法登录.报错如下&#xff1a; ERROR&#xff1a;ORA-28009: connection tosys shou…

NGINX平滑加权轮询算法

文章目录问题描述NGINX平滑加权轮询算法算法涉及的几个概念算法逻辑算法证明权重合理性平滑性问题描述 有三个节点{a, b, c}&#xff0c;它们的权重分别是{a5, b1, c1}&#xff0c;发送7次请求&#xff0c;要求&#xff1a; 根据权重分配&#xff1a;a会被分配5次&#xff0c…