-
C#教程之C#教程之我的第一个网络爬虫 C#版 福利 程序员专
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
最近在自觉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); }
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能