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. 总结的js性能优化方面的小知识

    前言 一直在学习javascript,也有看过<犀利开发Jquery内核详解与实践>,对这本书的评价只有两个字犀利,可能是对javascript理解的还不够透彻异或是自己太笨,更多的是自己 ...

  2. 源码安装Redis

    1.官网地址下载 猛击 mkdir /down cd down wgit http://download.redis.io/releases/redis-3.0.7.tar.gz ###准备工作:安装 ...

  3. Lock读写锁技术的妙用

    1.面试题1:三个线程读,三个线程写同一个数据 public class ReadWriteLockTest { public static void main(String[] args) { fi ...

  4. java 中变量的存储与引用

    java交换两个变量的值 1.幼儿园版 package tst; public class Test { public static void main(String[] args) { String ...

  5. 【poj2114】 Boatherds

    http://poj.org/problem?id=2114 (题目链接) 题意 给出一棵树,问是否存在两点间的距离为K. Solution 点分治嘛,跟poj1741差不多.. 然而为什么我调了一个 ...

  6. SQL Server数据同步的研究(单向/双向)

    思路: 1.做中间件(简单:定时采集:复杂:分布式,订阅中心的形式,如微信的中间件:https://github.com/tencent-wechat/phxsql) 2.采用触发器的形式,有数据触发 ...

  7. nginx ssl证书安装配置

    原理图: - 客户端生成一个随机数 random-client,传到服务器端(Say Hello) - 服务器端生成一个随机数 random-server,和着公钥,一起回馈给客户端(I got it ...

  8. select 1 from dual 中的1表示的含义

    select 1 from dual   在这条sql语句中的1代表什么意思?查出来是个什么结果?   其实: select 1 from table; select anycol(目的表集合中的任意 ...

  9. iOS 获得当前经纬度和城市

    1.引入CoreLocation.framework,#import <CoreLocation/CoreLocation.h>,添加委托CLLocationManagerDelegate ...

  10. zabbix 安装

    php+nginx+mysql+zabbix 官方https://www.zabbix.com/documentation/3.0/manual/installation/install 1.安装依赖 ...