using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; namespace MeiZi
{
public class Program
{
static void Main(string[] args)
{
new GetMeiziPic();
}
}
/// <summary>
/// 获取妹子图片
/// </summary>
public class GetMeiziPic
{
private readonly string _path;
private const string ImgRegex = @"<img[^>]*?src\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>";//图片的正则表达式
private const string LinkRegex = @"<h2><a\s+[^>]*?>[^<>]*?<\/a></h2>";
public GetMeiziPic()
{
_path = DealDir(Path.Combine(Environment.CurrentDirectory, "Images"));
Console.WriteLine("=============== 开始采集 ===============");
for (var i = ; i < ; i++)
{
Console.WriteLine("===============正在下载第{0}页数据===============", i);
DoFetchStep1(i);
} Console.WriteLine("=============== 采集完成 ===============");
}
private string DealDir(string path)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
return path;
}
private void DoFetchStep1(int pageNum)
{
var request = (HttpWebRequest)WebRequest.Create("http://www.sepaidui.com/?sort=4&page=" + pageNum);
request.Credentials = CredentialCache.DefaultCredentials;
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK) return;
var stream = response.GetResponseStream();
if (stream == null) return;
using (var sr = new StreamReader(stream))
{
FetchLinksFromSource1(sr.ReadToEnd());
}
} private void FetchLinksFromSource1(string htmlSource)
{
var matchesLink = Regex.Matches(htmlSource, LinkRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
foreach (Match m in matchesLink)
{
string href = m.Groups[].Value.Split('"')[];
DoFetchStep2(href);
}
} private void DoFetchStep2(string href)
{
var request = (HttpWebRequest)WebRequest.Create(href);
var h = request.Headers;
request.Credentials = CredentialCache.DefaultCredentials;
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK) return;
var stream = response.GetResponseStream();
if (stream == null) return;
using (var sr = new StreamReader(stream))
{
FetchLinksFromSource2(sr.ReadToEnd());
}
}
private void FetchLinksFromSource2(string htmlSource)
{
var matchesImgSrc = Regex.Matches(htmlSource, ImgRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
foreach (Match m in matchesImgSrc)
{
var href = m.Groups[].Value;
//只选取来自新浪相册的图片
if (href.Contains("sinaimg") && CheckIsUrlFormat(href) && !href.Contains("60d02b59tw1eq6g7srmiwj20pv03mdg8"))
{
Console.WriteLine(href);
}
else
continue;
using (var myWebClient = new WebClient())
{
try
{
myWebClient.DownloadFile(new Uri(href), Path.Combine(_path, Path.GetRandomFileName() + Path.GetExtension(href)));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
} private readonly Regex _isUrlFormat = new Regex(@"http://?([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
private bool CheckIsUrlFormat(string value)
{
return _isUrlFormat.IsMatch(value);
}
}
}

C#程序的更多相关文章

  1. JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  2. 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用

    有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...

  3. 微信小程序开发心得

    微信小程序也已出来有一段时间了,最近写了几款微信小程序项目,今天来说说感受. 首先开发一款微信小程序,最主要的就是针对于公司来运营的,因为,在申请appid(微信小程序ID号)时候,需要填写相关的公司 ...

  4. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  5. 微信应用号(小程序)开发IDE配置(第一篇)

    2016年9月22日凌晨,微信宣布“小程序”问世,当然只是开始内测了,微信公众平台对200个服务号发送了小程序内测邀请.那么什么是“小程序”呢,来看微信之父怎么说 看完之后,相信大家大概都有些明白了吧 ...

  6. 编写高质量代码:改善Java程序的151个建议(第5章:数组和集合___建议75~78)

    建议75:集合中的元素必须做到compareTo和equals同步 实现了Comparable接口的元素就可以排序,compareTo方法是Comparable接口要求必须实现的,它与equals方法 ...

  7. 【探索】在 JavaScript 中使用 C 程序

    JavaScript 是个灵活的脚本语言,能方便的处理业务逻辑.当需要传输通信时,我们大多选择 JSON 或 XML 格式. 但在数据长度非常苛刻的情况下,文本协议的效率就非常低了,这时不得不使用二进 ...

  8. 通过Jexus 部署 dotnetcore版本MusicStore 示例程序

    ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...

  9. Code Review 程序员的寄望与哀伤

    一个程序员,他写完了代码,在测试环境通过了测试,然后他把它发布到了线上生产环境,但很快就发现在生产环境上出了问题,有潜在的 bug. 事后分析,是生产环境的一些微妙差异,使得这种 bug 场景在线下测 ...

  10. 使用 .NET WinForm 开发所见即所得的 IDE 开发环境,实现不写代码直接生成应用程序

    直接切入正题,这是我09年到11年左右业余时间编写的项目,最初的想法很简单,做一个能拖拖拽拽就直接生成应用程序的工具,不用写代码,把能想到的业务操作全部封装起来,通过配置的方式把这些业务操作组织起来运 ...

随机推荐

  1. Python3 捕捉异常

    可以通过try/except语句来实现捕获异常,如下: bpython version 0.15 on top of Python 3.5.1+ /usr/bin/python3 >>&g ...

  2. Leetcode 198 House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  3. Leetcode 343. Integer Break

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  4. 如果把表单数据的校验交给了javascript那么后台还有没有必要对数据做校验呢

    现在很多同事,包括我,我表单的数据验证交给了javascript来做,那么新的问题来了,如果交给了javascript那么后台还有没有必要对数据做校验呢

  5. Hash_bzoj1862: [Zjoi2006]GameZ游戏排名系统

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  6. UTF-8有签名和无签名的区别

    当有签名的UTF-8编码内容被解析器解析时,解析器直接根据签名即可判断出使用UTF-8编码来进行解析,当无签名时,解析器会根据内容的编码来进行判别.所以,有签名的将更容易被解析器以正确的编码方式进行解 ...

  7. Swift 吐槽下Swift里一个逼死强迫症的语法:中缀语法

    中缀语法是OC里特有的一种,就是在函数的参数前面加一个解释词,让调用的时候明白该参数的含义 比如: -(void)processDataWithparamaA:(NSString *)paramaA ...

  8. ClassCastException 导致程序一运行就强制退出

    程序显示是类型转换异常,原因是  ic—launcher 不是layerlist图形,而是bmp类型(此处不是指格式)图片,因此取出来后经过红框内强制类型转换导致出错. 为什么我当时要把他强制转换? ...

  9. POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)

    题目链接: 传送门 Agri-Net Time Limit: 1000MS     Memory Limit: 10000K Description Farmer John has been elec ...

  10. JSContext

    An instance of JSContext represents a JavaScript execution environment. All JavaScript execution tak ...