C# 简单图片爬虫 快来斗图把

爬取后的图片默认在bin/debug/File下面

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 Crawler
{
    class Program
    {
        static void Main(string[] args)
        {
            //抓取网页资源 翻页url格式为https://fabiaoqing.com/biaoqing/lists/page/{i}.html 默认抓取200页
            for (int i = 1; i <= 200; i++)
            {
                string str = GetHtmlStr($"https://fabiaoqing.com/biaoqing/lists/page/{i}.html", "UTF8");
                //匹配图片的正则表达式    
                string regstr = "http://wx[1-4].sinaimg.cn/bmiddle/.+?.[jg][pi][fg]";
                foreach (Match match in Regex.Matches(str, regstr))
                //使用正则表达式解析网页文本,获得图片地址     
                {
                    //下载图片
                    SaveAsWebImg(match.Value);
                }
            }
            Console.ReadKey();
        }

        /// <summary>  
        /// 获取网页的HTML码  
        /// </summary>  
        /// <param name="url">链接地址</param>  
        /// <param name="encoding">编码类型</param>  
        /// <returns></returns>  
        public static string GetHtmlStr(string url, string encoding)
        {
            string htmlStr = "";
            if (!String.IsNullOrEmpty(url))
            {
                WebRequest request = WebRequest.Create(url);            //实例化WebRequest对象  
                WebResponse response = request.GetResponse();           //创建WebResponse对象  
                Stream datastream = response.GetResponseStream();       //创建流对象  
                Encoding ec = Encoding.Default;
                if (encoding == "UTF8")
                {
                    ec = Encoding.UTF8;
                }
                else if (encoding == "Default")
                {
                    ec = Encoding.Default;
                }
                StreamReader reader = new StreamReader(datastream, ec);
                htmlStr = reader.ReadToEnd();                           //读取数据  
                reader.Close();
                datastream.Close();
                response.Close();
            }
            return htmlStr;
        }

        /// <summary> 
        /// 下载网站图片 
        /// </summary> 
        /// <param name="picUrl"></param> 
        /// <returns></returns> 
        public static string SaveAsWebImg(string picUrl)
        {
            string result = "";
            string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"/File/";  //目录 
            //不存在目录则创建
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                DirectoryInfo dirInfo = new DirectoryInfo(path);
                dirInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
            }
            try
            {
                if (!String.IsNullOrEmpty(picUrl))
                {
                    Random rd = new Random();
                    DateTime nowTime = DateTime.Now;
                    string fileName = nowTime.Month.ToString() + nowTime.Day.ToString() + nowTime.Hour.ToString() + nowTime.Minute.ToString() + nowTime.Second.ToString() + rd.Next(1000, 1000000) + ".jpeg";
                    WebClient webClient = new WebClient();
                    webClient.DownloadFile(picUrl, path + fileName);
                    result = fileName;
                }
            }
            catch { }
            return result;
        }

    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值