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. ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet

    严重: Context initialization failedorg.springframework.beans.factory.BeanDefinitionStoreException: Fai ...

  2. 前端rem单位的使用研究

    分析网易新闻手机web端,http://3g.163.com/,发现里面大量使用了rem这个单位进行计算大小. 针对rem这个单位有如下解析: px:像素是相对于显示器屏幕分辨率而言的相对长度单位.p ...

  3. asp.net项目发布打包研究

    有几种思路: 1.[推荐]直接发布,然后手动打包成压缩包,需要的时候直接上传到服务器,或者在本地解压出来手动上传到虚拟空间(支持绝大多数的虚拟空间,自由度高,DZ也是采用这样的打包,FTP上传操作比较 ...

  4. NuGet在Push的时候提示“远程服务器返回错误:(403)已禁用”问题解决

    在使用NuGet把包push到nuget官网的时候,提示了如下信息: Failed to process request. 'The specified API key is invalid or d ...

  5. 洛谷P2246 SAC#1 - Hello World(升级版)

    题目背景 T1答案要mod1000000007(10^9+7),请重新提交,非常抱歉! 一天,智障的pipapi正在看某辣鸡讲义学程序设计. 题目描述 在讲义的某一面,他看见了一篇文章.这篇文章由英文 ...

  6. kickstart note

    KickStart是一种无人职守安装方式 PXE + kickstart + DHCP + TFTP 1.挂载光盘 mount /dev/sr0 /mnt/iso cp -rf /mnt/iso/*  ...

  7. CF 268E Playlist(贪心)

    题目链接: 传送门 Playlist time limit per test:1 second     memory limit per test:256 megabytes Description ...

  8. pycharm和输入法的冲突bug

    Solution:Either upgrade IBus to version 1.5.11 or add "export IBUS_ENABLE_SYNC_MODE=1" to ...

  9. STM8L --- External interrupt

    note 1:  Several interrupts can be pending at the same time. When an interrupt request is not servic ...

  10. 深入理解JavaScript中创建对象模式的演变(原型)

    深入理解JavaScript中创建对象模式的演变(原型) 创建对象的模式多种多样,但是各种模式又有怎样的利弊呢?有没有一种最为完美的模式呢?下面我将就以下几个方面来分析创建对象的几种模式: Objec ...