我的第一个网络爬虫 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就是在网上爬来爬去的蜘蛛.网络蜘蛛是通过网页的链接地址来寻找网页的. 从网站某一个 ...
随机推荐
- 验证二叉查找树 · Validate Binary Search Tree
[抄题]: [思维问题]: 不知道要定义resultType, 其实用仔细分析判断条件就行了:是否是bst+最大最小值 类似于平衡二叉树:是否平衡+左右的高度差 [一句话思路]: [输入量]:空: 正 ...
- [leetcode]283. Move Zeroes移零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- How to set an Apache Kafka multi node – multi broker cluster【z】
Set a multi node Apache ZooKeeper cluster On every node of the cluster add the following lines to th ...
- HTML5新协议介绍 WebSocket
WebSocket protocol 是HTML5一种新的协议(protocol).它是实现了浏览器与服务器全双工通信(full-duplex). 现在,很多网站为了实现即时通讯(real-time) ...
- Santa Claus and a Place in a Class
/* Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to t ...
- Codeforces 691C. Exponential notation 模拟题
C. Exponential notation time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...
- Devexpress VCL Build v2013 vol 14.1.3 发布
我修,我修,修修修. New Major Features in 14.1 What's New in VCL Products 14.1 Breaking Changes To learn abou ...
- 594. Longest Harmonious Subsequence
方法一:用一个map来辅助,代码简单,思路清晰 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }( ...
- 2018.09.30 bzoj2288:生日礼物(贪心+线段树)
传送门 线段树经典题目. 每次先找到最大子段和来更新答案,然后利用网络流反悔退流的思想把这个最大字段乘-1之后放回去. 代码: #include<bits/stdc++.h> #defin ...
- web大文件上传(web应用---SSH框架)
版权所有 2009-2018荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...