C# 从图片网站中查找符合要求的图片并按照特别要求存在本地硬盘

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;

namespace RegexPractice
{    

class program
    {
        static void Main(string[] args)
        {
            string entryPageUrl = "http://diggfoto.com/archive/?showall=1";
            string encodingName = "utf-8";

            List<PhotoInfo> photoList = PhotoInfo.ExtractPhotoList(entryPageUrl, encodingName);
            List<PhotoInfo> selectedPhotoList = new List<PhotoInfo>();

            foreach (PhotoInfo photoInfo in photoList)
            {
                DateTime date = new DateTime(int.Parse(photoInfo.Year), int.Parse(photoInfo.Month), int.Parse(photoInfo.Day));
                //if (date >= DateTime.Parse("2011-07-01"))
                //{
                    selectedPhotoList.Add(photoInfo);
                //}
            }

            photoList.Clear();

            foreach (PhotoInfo photoInfo in selectedPhotoList)
            {
                string photoUriPath = string.Format("http://diggfoto.com/{0}/{1}/{2}/{3}", photoInfo.Year, photoInfo.Month, photoInfo.Day, photoInfo.Path);
                string pageSource = Util.GetPageSource(photoUriPath, "utf-8");

                Regex regex = new Regex("title=.*?\" src=\"(?<sourcePath>.*?)\".*? width=\"(?<width>\\d{1,})\" height=\"(?<height>\\d{1,})\"");
                Match match = regex.Match(pageSource);
                if ((int.Parse(match.Groups["width"].Value) >= 1039)&&(int.Parse(match.Groups["height"].Value)>=737))
                {
                    string sourcePath = match.Groups["sourcePath"].Value;
                    byte[] pageSourceBytes = Util.GetPageSourceBytes(sourcePath);

                    if (!Directory.Exists(photoInfo.TargetSubDirPath))
                    {
                        Directory.CreateDirectory(photoInfo.TargetSubDirPath);
                    }
                    using (FileStream fs = new FileStream(photoInfo.FilePath, FileMode.Create, FileAccess.ReadWrite))
                    {
                        fs.Write(pageSourceBytes, 0, pageSourceBytes.Length);
                    }
                }
            }
        }
    }

    class Util
    {
        public static byte[] GetPageSourceBytes(string uri)
        {
            WebClient wc = new WebClient();
            byte[] pageSourceBytes = wc.DownloadData(new Uri(uri));
            return pageSourceBytes;
        }
        public static string GetPageSource(string uri, string encodingName)
        {
            byte[] pageSourceBytes = GetPageSourceBytes(uri);
            string pageSource = Encoding.GetEncoding(encodingName).GetString(pageSourceBytes);
            return pageSource;
        }
    }

    class PhotoInfo
    {
        public static Regex PhotoRegex = new Regex("'http://diggfoto.com/(?<year>\\d{4})/(?<month>\\d{2})/(?<day>\\d{2})/(?<path>.*?)/'.*?>(?<title>.*?)<");

        public static List<PhotoInfo> ExtractPhotoList(string url, string encodingName)
        {
            string pageSource = Util.GetPageSource(url, encodingName);
            MatchCollection mc = PhotoRegex.Matches(pageSource);
            List<PhotoInfo> photoList = new List<PhotoInfo>();

            foreach (Match match in mc)
            {
                PhotoInfo photoInfo = new PhotoInfo();
                photoInfo.Year = match.Groups["year"].Value;
                photoInfo.Month = match.Groups["month"].Value;
                photoInfo.Day = match.Groups["day"].Value;
                photoInfo.Path = match.Groups["path"].Value;
                photoInfo.Title = match.Groups["title"].Value;
                photoList.Add(photoInfo);
            }
            return photoList;
        }

        public string Year { get; set; }
        public string Month { get; set; }
        public string Day { get; set; }
        public string Path { get; set; }
        public string Title { get; set; }
        public string TargetSubDirPath
        {
            get
            { 
                return string.Format("c:\\{0}{1}{2}", Year, Month, Day);
            }
        }
        public string FilePath
        {
            get
            {
                return string.Format("{0}\\{1}.jpg",TargetSubDirPath,Title);
            }
        }

        public string GetTargetUrl()
        {
            return string.Format("http://diggfoto.com/{0}/{1}/{2}/{3}/", Year, Month, Day, Path);
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一.安装调试 当您下载下来源码后,请先解压,建议先本地用IIS调试(IIS安装方法见附件),如果无问题 1.修改数据库名称和路径 为了您网站的数据安全,请修改数据库名称或路径(App_Data\92design.asp)修改成自己命名 的文件(例如tt\haha.asp),同时也要修改相关文件的路径(inc\conn.asp 里面的数据库路径) 2.上传空间绑定域名 把测试过的源码用相关的FTP软件上传到您使用的空间,建议你绑定您的个性域名。详情请找 IDC空间服务商咨询。 二.使用操作 登录后台后,你可以通过左边的导航菜单来进行管理操作,如果有其它疑问可链接官方网站, 获得技术支持。 1.登录后台 在您的网址后面加上/admin,即可登录后台管理。默认管理员帐号admin,密码admin,登录后 请您修改你的密码。 2.设置您的站点属性 首页音乐播放:进入首页可选择是否自动播放您推荐的音乐 名称和网址:网站名称和网站地址 网站关键字:可以根据您站点的特点来填写,有利于百度各大搜索引擎搜索,让他/她更快的找到你 介绍和版权:站点介绍和版权信息 3.设置您的资料 您可以自由设定您的资料和交友意向,上传您的个人靓照。 4.查看想跟你交往的朋友 查看看见他/她的照片相关个人资料和联系方式。 5.写文章日记 可以添加2级分类,文章可以输入密码加密,别人想看你的日记,必须有你设置的密码才行哦 。 6.传相册图片 可以添加2级分类,前台页面可以访问用户可以切换自动播放,并设定时间。 7.添加多媒体文件 支持音乐,视频,FLASH播放,音乐可以添加歌词,并可以设置首页自动播放的音乐.可直接 填写网址链接,如果文件太大建议先传FTP.(注意:要点击生成音乐播放列表才生效) 8.工具箱 收藏夹分类可以收集平时常用的一些网址,网络硬盘用于储存你常用的一些文件,容量 大小根据你的空间而定,并可以公开下载,或者加密,通过密码进行下载。 9.留言版 查看用户的留言,支持悄悄话功能,可回复留言

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值