mini爬虫程序

复制代码
Code
class MiniCrawler
    {
        
// Find a link in a content string.
        static string FindLink(string htmlstr, ref int startloc)
        {
            
int i;
            
int start, end;
            
string uri = null;
            
string lowcasestr = htmlstr.ToLower();
            i 
= lowcasestr.IndexOf("href=\"http", startloc);
            if (i != -1)
            {
                start 
= htmlstr.IndexOf('"', i) + 1;
                end 
= htmlstr.IndexOf('"', start);
                uri 
= htmlstr.Substring(start, end - start);
                startloc 
= end;
            }
            
return uri;

        }
        
public static void Crawle(string uristr)
        {
            
string link = null;
            
string str;
            
string answer;
            
int curloc; // holds current location in response
            try
            {
                
do
                {
                    Console.WriteLine(
"Linking to " + uristr);
                    
// 创建一个指定URI的WebRequest
                    HttpWebRequest req = (HttpWebRequest)
                    WebRequest.Create(uristr);

                    
// 发送reques得到返回的response.
                    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                    
// 从返回的内容中获得数据流
                    Stream istrm = resp.GetResponseStream();

                    StreamReader rdr 
= new StreamReader(istrm);
                    
// 读取整个页面
                    str = rdr.ReadToEnd();
                    curloc 
= 0;
                    
do
                    {
                        
// 查找下一个uri
                        link = FindLink(str, ref curloc);
                        
if (link != null)
                        {
                            Console.WriteLine(
"发现链接: " + link);
                            Console.Write(
"Link, More, Quit?");
                            answer 
= Console.ReadLine();
                            
if (string.Compare(answer, "L"true== 0)
                            {
                                uristr 
= string.Copy(link);
                                
break;
                            }
                            
else if (string.Compare(answer, "Q"true== 0)
                            {
                                
break;
                            }
                            
else if (string.Compare(answer, "M"true== 0)
                            {
                                Console.WriteLine(
"Searching for another link.");
                            }
                        }
                        
else
                        {
                            Console.WriteLine(
"No link found.");
                            
break;
                        }
                    } 
while (link.Length > 0);
                    
// Close the response.
                    resp.Close();
                } 
while (uristr != null);
            }
            
catch (WebException exc)
            {
                Console.WriteLine(
"Network Error: " + exc.Message +
                
"\nStatus code: " + exc.Status);
            }
            
catch (ProtocolViolationException exc)
            {
                Console.WriteLine(
"Protocol Error: " + exc.Message);
            }
            
catch (UriFormatException exc)
            {
                Console.WriteLine(
"URI Format Error: " + exc.Message);
            }
            
catch (NotSupportedException exc)
            {
                Console.WriteLine(
"Unknown Protocol: " + exc.Message);
            }
            
catch (IOException exc)
            {
                Console.WriteLine(
"I/O Error: " + exc.Message);
            }
            Console.WriteLine(
"Terminating MiniCrawler.");
        }
    }
复制代码
posted @   你听海是不是在笑  阅读(609)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示