.Net 使用爬虫下载网络图片到本地磁盘

准备:

1.新建控制台项目

2.引用System.Drawing类库

3.安装HtmlAgilityPack 1.5.2.0

4.如果不会XPath语法的话,建议简单看下

代码:

 1 static void Main(string[] args)
 2         {
 3             #region 爬虫测试
 4             {
 5                 //此处为将要解析的URL,可设置为参数变量(我这里把URL写死了)
 6                 string html = HttpHelper.DownloadHtml(@"http://wyxa.googlefilm.com.cn/wenzhang/info_386854_w821759016~i5.html", Encoding.UTF8);
 7                 HtmlDocument doc = new HtmlDocument();
 8 
 9                 //由于本人要解析的网址,HTMl标签是动态生成的,所以不能将XPath表达式一次性写出,只能先解析出来非动态标签,再一步一步解析动态生成的标签.
10                 doc.LoadHtml(html);//加载html(此时要解析的标签是非动态生成的,很好获取)
11                 string pageNumberPath = @"//table[2]";
12                 HtmlNode pageNumberNode = doc.DocumentNode.SelectSingleNode(pageNumberPath);
13 
14                 //拿到以上获取的非动态标签之后,再解析里边的动态生成的标签,所以要再次LoadHtml一下
15                 doc.LoadHtml(pageNumberNode.InnerHtml);
16                 pageNumberPath = "//div[@id='div_text']";
17                 pageNumberNode = doc.DocumentNode.SelectSingleNode(pageNumberPath);
18 
19                 //同理,解析动态标签,再次LoadHtml
20                 doc.LoadHtml(pageNumberNode.InnerHtml);
21                 pageNumberPath = "//img";
22                 HtmlNodeCollection pageNumberNodes = doc.DocumentNode.SelectNodes(pageNumberPath);
23 
24                 //获取到需要的img标签集合后,遍历,获取src地址
25                 foreach (var item in pageNumberNodes)
26                 {
27                     if (item.Attributes["data-src"] != null)
28                     {
29                         var imgSrc = item.Attributes["data-src"].Value;
30 
31                         //控制台打印src地址
32                         Console.WriteLine(imgSrc);
33 
34                         //通过解析出来的src地址下载图片
35                         DownloadPhotoFromUrl(imgSrc);
36                     }
37                 }
38             }
39 }
HttpHelper.DownloadHtml方法
public static string DownloadHtml(string url,Encoding encoding)
{
  HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
  myReq.Timeout = 24000;
  HttpWebResponse httpWResp = (HttpWebResponse)myReq.GetResponse();
  Stream myStream = httpWResp.GetResponseStream();
  if (myStream != null)
  {
    StreamReader sr = new StreamReader(myStream, encoding);
    return sr.ReadToEnd();
  }
  return "";
}

 

下载网络图片的方法:

 1       /// <summary>
 2         /// 从图片地址下载图片到本地磁盘
 3         /// </summary>
 4         /// <param name="Url">图片网址</param>
 5         /// <returns></returns>
 6         public static void DownloadPhotoFromUrl(string Url)
 7         {
 8             HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(Url);
 9             HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
10             if (webresponse.StatusCode == HttpStatusCode.OK)
11             {
12                 System.Drawing.Image image = System.Drawing.Image.FromStream(webresponse.GetResponseStream());
13                 //保存在本地文件夹
14                 image.Save(@"D:\\images\"+DateTime.Now.ToString("yyyyMMddHHmmssff")+".jpg");
15                 //释放资源
16                 image.Dispose(); 
17             }
18         }

至此,功能完成.

以下为本人控制台打印结果:

图片下载截图:

本人菜鸟,只是为了记录学习中的小知识点,请大神勿喷~

posted @ 2018-10-24 15:32  一叶、知秋  阅读(1100)  评论(2编辑  收藏  举报