http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3169|回复: 1

[其他] 大神们帮我看下为什么我采集不到文章呢?我远行就报错

[复制链接]
发表于 2014-5-2 13:48:55 | 显示全部楼层 |阅读模式
20金钱
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace code20
{
    class Program
    {
        static void Main(string[] args)
        {


            Console.Write("1:抓取,2:处理:");
            if (Console.ReadLine() == "1")
            {





                string url = "http://www.admin5.com/article/20121228/481070.shtml";
                string html=  gethtml(url, Encoding.Default);

                //Regex r = new Regex("(?<=<title>).*?(?=</title>)");//实例化一个正则
                //MatchCollection co=r.Matches(html);//匹配所有项返回一个集合
                //Console.WriteLine("标题:" + co[0].Value);//通过索引加value获取到内容


                //Regex rcontent = new Regex("<div class=\"content\">[\\s\\S]*?</div>");
                //MatchCollection cocontent = rcontent.Matches(html);
                //Console.WriteLine("内容:"+cocontent[0].Value);


                string listurl = "http://www.admin5.com/browse/177/";



                string listhtml = gethtml(listurl, Encoding.Default);
                //http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
                Regex rlist = new Regex("(?<=href=\").*?(?=\")");

                MatchCollection co = rlist.Matches(listhtml);

                for (int i = 0; i < co.Count; i++)
                {

                    if (co[i].Value.ToString().Contains("article"))
                    {


                        Console.WriteLine("http://www.admin5.com/" + co[i].Value);
                        Console.WriteLine("抓取内容....");
                        string contenthtml = gethtml("http://www.admin5.com/" + co[i].Value, Encoding.Default);

                        Regex r = new Regex("(?<=<title>).*?(?=</title>)");//实例化一个正则
                        MatchCollection cotitlt = r.Matches(contenthtml);//匹配所有项返回一个集合

                        // Console.WriteLine("标题:" + cotitlt[0].Value);//通过索引加value获取到内容


                        Regex rcontent = new Regex("<div class=\"content\">[\\s\\S]*?</div>");
                        MatchCollection cocontent = rcontent.Matches(contenthtml);
                        // Console.WriteLine("内容:" + cocontent[0].Value);
                        string title = cotitlt[0].Value;
                        string content = cocontent[0].Value;
                        Console.WriteLine("保存数据...");
                        string appdir = Directory.GetCurrentDirectory();
                        if (!Directory.Exists(appdir + "\\data"))
                        {
                            Directory.CreateDirectory(appdir + "\\data");

                        }


                        File.WriteAllText(appdir + "\\data" + "\\" + i + ".txt", title + "\r\n" + content);
                        Console.WriteLine("保存成功!");



                    }

                }

                Console.ReadLine();

            }
            else
            {

                string appdir = Directory.GetCurrentDirectory();

                string [] files= Directory.GetFiles(appdir+"\\data");//获取data里面所有的文件

                foreach(string filename in files)//遍历所有文件名
                {
                    Console.WriteLine(filename);

                    string html = File.ReadAllText(filename,Encoding.UTF8);//读取内容
                    string title = html.Remove(html.IndexOf('\n')); //提取标题
                    string content = html.Replace(title, "");//替换掉内容中的标题,提取出内容
                     title = title.Remove(title.LastIndexOf('-'));//处理标题
                    Console.Write(title);
                  

                    Regex r = new Regex("(?<=href=\").*?(?=\")");

                   MatchCollection con= r.Matches(content);
                   for (int i = 0; i < con.Count;i++ )
                   {

                       string url = con[i].Value;
                      string newurl =url.Replace( "www.admin5.com","www.shouyu.com");
                      content=   content.Replace(url,newurl);
                     
                  
                   }
           content = content.Replace("<div class=\"content\">","").Replace("</div>","");
                  







                    Console.WriteLine("保存...");
                    File.Delete(filename);
                    File.WriteAllText(filename, title + "\r\n" + content, Encoding.UTF8);

                    Console.ReadLine();
                }

                Console.ReadLine();
            
            }


        }
        /// <summary>
        /// 根据url和编码获取html内容
        /// </summary>
        /// <param name="url">完整链接带http</param>
        /// <param name="enc">编码</param>
        /// <returns>字符串,html代码</returns>
        public static string gethtml(string url,Encoding enc)
        {

            WebClient myweb = new WebClient();//实例化一个WebClient连接

            Stream stream = myweb.OpenRead(url);//根据指定的url获取流

            StreamReader sr = new StreamReader(stream,enc);//从流中用utf8编码实例化一个读取器

            string html = sr.ReadToEnd();//从流中读取数据得到字符串

            return html;
        
        }
    }
}



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2015-1-6 11:54:33 | 显示全部楼层
我只是路过打酱油的。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-10 07:37

© 2014-2021

快速回复 返回顶部 返回列表