【问题标题】:How to fix my crawler in C#?如何在 C# 中修复我的爬虫?
【发布时间】:2010-02-26 13:21:07
【问题描述】:
Regex hrefs = new Regex("<a href.*?>");
Regex http = new Regex("http:.*?>");  
StringBuilder sb = new StringBuilder();
WebClient client = new WebClient();
string source = client.DownloadString("http://google.com");
foreach (Match m in hrefs.Matches(source)){
sb.Append(http.Match(m.ToString()));
Console.WriteLine(http.Match(m.ToString()));
}

代码工作正常,但只有一次问题 看看输出。

http://images.google.se/imghp?hl=sv&tab=wi" onclick=gbar.qs(this) class=gb1>
http://video.google.se/?hl=sv&tab=wv" onclick=gbar.qs(this) class=gb1>
http://maps.google.se/maps?hl=sv&tab=wl" onclick=gbar.qs(this) class=gb1>
http://news.google.se/nwshp?hl=sv&tab=wn" onclick=gbar.qs(this) class=gb1>
http://translate.google.se/?hl=sv&tab=wT" onclick=gbar.qs(this) class=gb1>
http://mail.google.com/mail/?hl=sv&tab=wm" class=gb1>
http://www.google.se/intl/sv/options/" onclick="this.blur();gbar.tg(event);return !1" aria-haspopup=true class=gb3>
http://blogsearch.google.se/?hl=sv&tab=wb" onclick=gbar.qs(this) class=gb2>
http://www.youtube.com/?hl=sv&tab=w1&gl=SE" onclick=gbar.qs(this) class=gb2>
http://www.google.com/calendar/render?hl=sv&tab=wc" class=gb2>
http://picasaweb.google.se/home?hl=sv&tab=wq" onclick=gbar.qs(this) class=gb2>
http://docs.google.com/?hl=sv&tab=wo" class=gb2>
http://www.google.se/reader/view/?hl=sv&tab=wy" class=gb2>
http://sites.google.com/?hl=sv&tab=w3" class=gb2>
http://groups.google.se/grphp?hl=sv&tab=wg" onclick=gbar.qs(this) class=gb2>
http://www.google.se/ig%3Fhl%3Dsv%26source%3Diglk&usg=AFQjCNEsLWK4azJkUc3KrW46JTUSjK4vhA" class=gb4>
http://www.google.se/" class=gb4>
http://www.google.com/intl/sv/landing/games10/index.html">
http://www.google.com/ncr">

如何删除 html 标签?

【问题讨论】:

    标签: c# .net web-crawler


    【解决方案1】:

    将您的正则表达式更改为:

    Regex http = new Regex("http:.*?\"");
    

    或者更好的是,使用 HtmlAgilityPack 和 Xpath 解析所有链接:

    var web = new HtmlWeb();
    var doc = web.Load("http://www.stackoverflow.com");
    
    var nodes = doc.DocumentNode.SelectNodes("//a[@href]"); // Will find all links
    
    foreach (var node in nodes)
    {
        Console.WriteLine(node.InnerHtml);
    }
    

    【讨论】:

    • +1 用于建议非正则表达式解决方案。解析现实世界的 HTML 可能很棘手,我认为正则表达式是 90% 的解决方案。如果我需要比这更防弹的东西,我会使用专为手头任务设计的工具。
    • 用正则表达式解析 HTML 很糟糕。除非您需要性能,否则请选择可以正确完成工作的东西
    【解决方案2】:

    快速的解决方案是改变这个:

    Regex http = new Regex("http:.*?>");
    

    到这里:

    Regex http = new Regex("http:.*?\"");
    

    更好的解决方案是使用库来解析 html - HTML Agility Pack 可用于此目的,这将使您的生活更轻松。

    【讨论】:

      【解决方案3】:

      将以下行 http.Match(m.ToString()) 子串到 http.Match(m.ToString().remove(m.ToString().IndexOf("\"")))

      不是最干净的方法,但它有效

      【讨论】:

        【解决方案4】:

        将结束标签改为"

        【讨论】:

          【解决方案5】:
          Regex hrefs = new Regex("<a href.*?>");
          Regex http = new Regex("(http:.*?)\"");  
          StringBuilder sb = new StringBuilder();
          WebClient client = new WebClient();
          string source = client.DownloadString("http://google.com");
          foreach (Match m in hrefs.Matches(source))
          {
              var value = http.Match(m.ToString()).Groups[1].Value;
              sb.Append(value);
              Console.WriteLine(value);
          }
          

          【讨论】:

            【解决方案6】:

            一个不错且简单的解决方案。匹配 http: 之后的任意字符,除了 " 字符

            "http:[^\"]*"
            

            【讨论】:

              【解决方案7】:

              文件和图像:&lt;\s*(?&lt;Tag&gt;(applet|embed|frame|img|link|script|xml))\s*.*?(?&lt;AttributeName&gt;(src|href|xhref))\s*=\s*[\"\'](?&lt;FileOrImage&gt;.*?)[\"\']

              超链接:&lt;\s*(?&lt;Tag&gt;(a|form|frame))\s*.*?(?&lt;AttributeName&gt;(action|href|src))\s*=\s*[\"\'](?&lt;HyperLink&gt;.*?)[\"\']

              【讨论】:

              • * 字符使事物变为斜体,&lt; 字符被解释为 HTML。使用 ` 保护您的文本。 Formatting help.
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-11-07
              • 2013-08-27
              • 1970-01-01
              • 2023-03-05
              • 2015-07-12
              相关资源
              最近更新 更多