我来做百科(第二十天) B

来源:互联网 发布:淘宝货款可以提现吗 编辑:程序博客网 时间:2024/05/01 01:03
tag系统完成,再修复一些添加词条,修改内容的问题,就可以做数据采集了。

数据采集网上的资料很多,再结合自己的需要,写了一下代码:
protected void Button1_Click(object sender, EventArgs e)
    
{
        Lemma lemma 
= new Lemma();

        Response.Write(
"采集结果:<br/><br/>");
        Response.Flush();

        
for (int i = 0; i <= 3; i++)
        
{

            
string sUrl = strurl + (i * 10).ToString();

            Response.Write(
"采集url:" + sUrl + "<br/>");
            Response.Flush();

            
foreach (string temp in GetHtmls(@"/view//d+/.htm", GetUrlHtml(sUrl)))
            
{
                
string url = u + temp;

                
string sHtml = GetUrlHtml(url);

                
string sLemma = GetLemma(sHtml);
                
string sDetail = GetDetail(sHtml);
                
string sTag = GetTag(sHtml);

                
int idLemma = lemma.AddLemma(sLemma, sDetail, "cloud"0string.Empty, url, sTag);

                StringBuilder sb 
= new StringBuilder();
                sb.Append(
"id:").Append(idLemma).Append("<br/> 词条:").Append(sLemma).Append("<br/>");
                sb.Append(
"Tag:").Append(sTag).Append("<br/> 连接:<a href='").Append(url).Append("'' target='_blank'>").Append(url).Append("</a><br/>");

                
if (idLemma > 0)
                
{
                    sb.Append(
"成功!").Append(" <a href='../index/show.aspx?id=").Append(idLemma).Append("' target='_blank'>查看</a>"); ;
                }

                
else
                
{
                    sb.Append(
"失败!错误代码:").Append(idLemma);
                }


                sb.Append(
"<br/><br/>");

                Response.Write(sb.ToString());
                Response.Flush();
            }

        }

    }


    
public static string GetUrlHtml(string url)
    
{
        
string output = "";
        Encoding encode 
= Encoding.Default;
        WebClient webclient 
= new WebClient();
        
try
        
{
            webclient.Headers.Add(
"Referer", url);
            
byte[] buff = webclient.DownloadData(url);
            output 
= encode.GetString(buff);
        }

        
catch
        
{
        }

        
return output;
    }


    
public static string GetHtml(string begin, string end, string content)
    
{
        
return GetHtml(begin + "((.*?//n?)*?)" + end, content);
    }


    
public static string GetHtml(string pattern, string content)
    
{
        Regex reg 
= new Regex(pattern);
        Match match 
= reg.Match(content);

        
if (match != Match.Empty)
        
{
            
//content = content.Replace(match.Groups[1].ToString(), string.Empty);
            return match.Groups[1].ToString();
        }

        
else
        
{
            
return string.Empty;
        }

    }


    
public static StringCollection GetHtmls(string begin, string end, string content)
    
{
        
return GetHtmls(begin + "((.*?//n?)*?)" + end, content);
    }


    
public static StringCollection GetHtmls(string pattern, string content)
    
{
        Regex reg 
= new Regex(pattern);
        MatchCollection matches 
= reg.Matches(content);
        StringCollection list 
= new StringCollection();
        
foreach (Match match in matches)
        
{
            
if (match != Match.Empty)
            
{
                list.Add(match.Value);
            }

        }

        
return list;
    }


    
/// <summary>
    
/// 正则替换
    
/// </summary>

    public static string ReplaceText(string input, string pattern, string replacement)
    
{
        
if (string.IsNullOrEmpty(input)) return string.Empty;
        Regex rgx 
= new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
        
return rgx.Replace(input, replacement);
    }


    
/// <summary>
    
/// 去标签 包括内容
    
/// </summary>

    public static string ClearWholeTag(string input, string tag)
    
{
        
return ReplaceText(input, @"<" + tag + "[^>]*?>.*?</" + tag + ">""");
    }


    
/// <summary>
    
/// 去标签 不包括内容
    
/// </summary>

    public static string ClearTag(string input, string tag)
    
{
        
return ReplaceText(input, @"<//?" + tag + "[^>]*>""");
    }


    
/// <summary>
    
/// 去全部标签
    
/// </summary>

    public static string ClearAllTag(string input)
    
{
        
return ReplaceText(input, @"<//?[a-zA-Z]+[^>]*>""");
    }


数据采集就是爽,先来三百多条吧,哈哈。
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孕妇尿细菌很高怎么办 尿微量总蛋白高怎么办 衣服上碳素笔油怎么办 卡油泵总成坏了怎么办 汽车燃油泵坏了怎么办 新车汽油泵坏了怎么办 墓地的树枯死了怎么办 大车尿素不烧了怎么办 不烧尿素限扭怎么办 汽车首保过期了怎么办 假体隆鼻后透光怎么办 熊猫血有抗体了怎么办 rh阴性血怀二胎怎么办 熊猫血怀二胎了怎么办 rh阴性血要二胎怎么办 全血粘度都偏高怎么办 血脂高血粘度高怎么办 全血粘度值1偏高怎么办 全血粘度3偏高怎么办 血粘度高的症状怎么办 粉瘤感染化脓了怎么办 乌药剂量用大了怎么办 水卡消磁了怎么办妙招 电卡消磁了怎么办妙招 入园磁卡消磁了怎么办 透析中静脉压高怎么办 腰间盘突出压迫神经腿疼怎么办 肺热引起的发烧怎么办 肺热引起的痘痘怎么办 冰箱压条的霉点怎么办 白色的布鞋变黄怎么办 白鞋橡胶变黄了怎么办 肝功能检查总胆汁酸高怎么办 吃丹参滴丸尿血怎么办 胰酶消化过久怎么办 抗凝血酶活性低怎么办 抗凝血酶3偏低怎么办 抗凝血酶ⅲ低 怎么办 抗凝血酶3抗原高怎么办 孕32周血压高怎么办 智齿血凝块掉了怎么办