我的第一個網路爬蟲 C#版 福利 程式員專車

來源:互聯網
上載者:User

標籤:read   request   message   color   .com   oid   adt   看到了   parse   

最近在自覺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);        }

 

我的第一個網路爬蟲 C#版 福利 程式員專車

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.