我的第一个网络爬虫 C#版 福利 程序员专车
最近在自觉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 = ; 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();
} } private static uint ReadStartIndex()
{
while (true)
{
Console.Write("Set start index :"); string line = Console.ReadLine(); uint index = ; if (UInt32.TryParse(line, out index))
{
Console.WriteLine("Start index setted : "+ index);
return index;
} Thread.Sleep();
}
} private static uint ReadLoopLen()
{
while (true)
{
Console.Write("Set loop len :"); string line = Console.ReadLine(); uint index = ; if (UInt32.TryParse(line, out index))
{
Console.WriteLine("Loop len setted : " + index);
return index;
} Thread.Sleep();
}
} 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#版 福利 程序员专车的更多相关文章
- 用Python写网络爬虫 第二版
书籍介绍 书名:用 Python 写网络爬虫(第2版) 内容简介:本书包括网络爬虫的定义以及如何爬取网站,如何使用几种库从网页中抽取数据,如何通过缓存结果避免重复下载的问题,如何通过并行下载来加速数据 ...
- 【网络爬虫入门01】应用Requests和BeautifulSoup联手打造的第一条网络爬虫
[网络爬虫入门01]应用Requests和BeautifulSoup联手打造的第一条网络爬虫 广东职业技术学院 欧浩源 2017-10-14 1.引言 在数据量爆发式增长的大数据时代,网络与用户的沟 ...
- 使用Python写的第一个网络爬虫程序
今天尝试使用python写一个网络爬虫代码,主要是想訪问某个站点,从中选取感兴趣的信息,并将信息依照一定的格式保存早Excel中. 此代码中主要使用到了python的以下几个功能,因为对python不 ...
- 爬虫神器XPath,程序员带你免费获取周星驰等明星热门电影
本教程由"做全栈攻城狮"原创首发,本人大学生一枚平时还需要上课,但尽量每日更新文章教程.一方面把我所习得的知识分享出来,希望能对初学者有所帮助.另一方面总结自己所学,以备以后查看. ...
- 《用Python写爬虫》学习笔记(二)编写第一个网络爬虫
1.首先,下载网页使用Python的urllib2模块,或者Python HTTP模块request来实现 urllib2会出现问题,解决方法1.重试下载(设置下载次数) 2.设置用户代理 2.其次, ...
- Delphi-网络编程-第一个网络方面作品(UDP聊天程序)
其实这不算是一个聊天程序,因为还不能实现双方互发信息,只有一方能发信息,呵呵 我以后再改进吧.... 服务端代码: unit Unit1; interface uses Windows, ...
- Python网络爬虫
http://blog.csdn.net/pi9nc/article/details/9734437 一.网络爬虫的定义 网络爬虫,即Web Spider,是一个很形象的名字. 把互联网比喻成一个蜘蛛 ...
- 【Python开发】【神经网络与深度学习】网络爬虫之python实现
一.网络爬虫的定义 网络爬虫,即Web Spider,是一个很形象的名字. 把互联网比喻成一个蜘蛛网,那么Spider就是在网上爬来爬去的蜘蛛. 网络蜘蛛是通过网页的链接地址来寻找网页的. 从网站某一 ...
- [Python]网络爬虫(一):抓取网页的含义和URL基本构成
一.网络爬虫的定义 网络爬虫,即Web Spider,是一个很形象的名字. 把互联网比喻成一个蜘蛛网,那么Spider就是在网上爬来爬去的蜘蛛.网络蜘蛛是通过网页的链接地址来寻找网页的. 从网站某一个 ...
随机推荐
- 没有无线路由,如何让手机使用电脑的网络xyytit
前言: 智能手机已经越来越普遍,但国内的无线网络的步伐还是没有跟上智能机的脚步.纵使3G,4G已经相继推出,但国内的资费价格着实有点不接地气,所以无线wifi无疑是智能机使用最多的.各大软件上.设备商 ...
- iOS9 UIWindow rootViewController
在iOS9中App被其他应用唤起的时候Crash,正常启动或者调试模式都不会Crash. 通过XCode - Window -Device,查看设备的log,如下 Assertion failure ...
- fragment Activity之间传值的方法 之------------接口回调
首先 定义一个 回调接口 public interface FragmentCallBack { public void callbackFun1(Bundle arg); public void ...
- c语言使用指针实现模拟java/c# string.concat字符串串联方法
#include <stdio.h> void _strcat(char *, const char *); int main(void) { char source[] ="V ...
- IDEA 的VM Option设置加快页面的加载速度
VM Option的设置: -Xms1024M -Xmx2048M -XX:PermSize=128M -XX:MaxPermSize=256M
- pip常用操作指令
1.安装模块 pip install vitualenv pip install -r requirement.txt 2.查询模块信息 pip show pip 3.显示已经安装的模块 pip li ...
- Codeforces 709C 模拟
C. Letters Cyclic Shift time limit per test:1 second memory limit per test:256 megabytes input:stand ...
- ubuntu关闭防火墙
https://jingyan.baidu.com/article/73c3ce283ee2c1e50343d9f6.html
- inline 引起undefined reference to
main.cc:57: undefined reference to `evpp::udp::UdpDecoder::GetHeader()'collect2: error: ld returned ...
- java中配置JPA方法
JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中 使用JPA进行保存对象时,可以用对象来接收,例 ...