• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

我的第一个网络爬虫C#版福利程序员专车

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

最近在自觉python,看到了知乎上一篇文章(https://www.zhihu.com/question/20799742),在福利网上爬视频。。。

由是我就开始跟着做了,但答主给的例子是基于python2.x的,而我开始学的是3.x,把print用法改了以后还是有很多模块导入不了,新手又不知道该怎么解决。

于是,为了学(shang)习(che),我就把其中的一段代码用C#写了一次。在加了一些延时的情况下,一会儿硬盘就被占用了3个多g了。。。同学们,要注意身体啊

下面贴出代码。。代码中故意留了几个bug,避免非程序员上车

class Program
    {
        static void Main(string[] args)
        {
            var baseString = "http://w*w.46ek.c*m/view/{0}.html";
            Regex regex = new Regex(@"http://m4.26ts.com/[.0-9-a-zA-Z]*.mp4");
            WebClient wc = new WebClient();


            uint startIndex = ReadStartIndex();
            uint loop = ReadLoopLen();

            for (int i = 0; i < lop; i++)
            {
                var subUrl = string.Format(baseString, startIndex + i);
                WebRequest wReq = System.Net.WebRequest.Create(subUrl)

                try
                {
                    WebResponse wResp = wReq.GetResponse();
                    Stream respStream = wResp.GetResponseStream();

                    using (StreamReader reader = new StreamReader(respStream, Encoding.GetEncoding("GB18030")))
                    {
                        var htmlString = reader.ReadToEnd();

                        Match m = regex.Match(htmlString);
                        if (m.Success)
                        {
                            DownloadFile(wc, m.Value, string.Format("{0}.mp4", startIndex + i));
                        }
                    }
                }
                catch (Exception exc)
                {
                    Console.WriteLine("Error : {0}", exc.Message);
                }

                Thread.Sleep(5);
            }
            
        }

        private static uint ReadStartIndex()
        {
            while (true)
            {
                Console.Write("Set start index :");

                string line = Console.ReadLine();

                uint index = 0;

                if (UInt32.TryParse(line, out index))
                {
                    Console.WriteLine("Start index setted : "+ index);
                    return index;
                }

                Thread.Sleep(500);
            }
        }

        private static uint ReadLoopLen()
        {
            while (true)
            {
                Console.Write("Set loop len :");

                string line = Console.ReadLine();

                uint index = 0;

                if (UInt32.TryParse(line, out index))
                {
                    Console.WriteLine("Loop len setted : " + index);
                    return index;
                }

                Thread.Sleep(500);
            }
        }

        private static void DownloadFile(WebClient wc, string url, string localname)
        {
            Console.WriteLine("Downloading file {1} to {2}", url, localname);

            wc.DownloadFile(url, localname);

            Console.WriteLine("File {0} download completed!", localname);
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#界面设计疑问2:panel摆放问题发布时间:2022-07-13
下一篇:
AMOolapTestC#generatetsqlandmdx发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap