.net downloads web page source code, downloads network pictures or IIS pictures to local, and copies LAN pictures to local

It can be used to download pictures on the network. If you copy pictures from the LAN to the local area, first set the picture directory of the source server to be copied as an IIS site, and then you can download and copy pictures as network pictures to the local area. For example: l source server pictures The root directory is Image, just point IIS to Image. The image disk path of the source server is: d:\Image\202306\01\0934070Test Seven\Tupian.jpg. It is deployed to IIS. http://192.168.1.102:81/202306/01/0934070 Test Seven/Tupian.jpg

Prepare:

1. Create a new console project

2. Reference the System.Drawing class library

3. Install HtmlAgilityPack 1.5.2.0

4. If you don’t know the XPath syntax, it is recommended to simply look at it

 static void Main(string[] args)
 {
	 #region 爬虫测试
	 {
		 //此处为将要解析的URL,可设置为参数变量(我这里把URL写死了)
		 string html = HttpHelper.DownloadHtml(@"http://wyxa.googlefilm.com.cn/wenzhang/info_386854_w821759016~i5.html", Encoding.UTF8);
		 HtmlDocument doc = new HtmlDocument();

		 //由于本人要解析的网址,HTMl标签是动态生成的,所以不能将XPath表达式一次性写出,只能先解析出来非动态标签,再一步一步解析动态生成的标签.
		 doc.LoadHtml(html);//加载html(此时要解析的标签是非动态生成的,很好获取)
		 string pageNumberPath = @"//table[2]";
		 HtmlNode pageNumberNode = doc.DocumentNode.SelectSingleNode(pageNumberPath);

		 //拿到以上获取的非动态标签之后,再解析里边的动态生成的标签,所以要再次LoadHtml一下
		 doc.LoadHtml(pageNumberNode.InnerHtml);
		 pageNumberPath = "//div[@id='div_text']";
		 pageNumberNode = doc.DocumentNode.SelectSingleNode(pageNumberPath);

		 //同理,解析动态标签,再次LoadHtml
		 doc.LoadHtml(pageNumberNode.InnerHtml);
		 pageNumberPath = "//img";
		 HtmlNodeCollection pageNumberNodes = doc.DocumentNode.SelectNodes(pageNumberPath);

		 //获取到需要的img标签集合后,遍历,获取src地址
		 foreach (var item in pageNumberNodes)
		 {
			 if (item.Attributes["data-src"] != null)
			 {
				 var imgSrc = item.Attributes["data-src"].Value;

				 //控制台打印src地址
				 Console.WriteLine(imgSrc);

				 //通过解析出来的src地址下载图片
				 DownloadPhotoFromUrl(imgSrc);
			 }
		 }
	 }
 }
HttpHelper.DownloadHtml method
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 "";
}

How to download pictures from the Internet:

        /// <summary>
        /// 从图片地址下载图片到本地磁盘
        /// </summary>
        /// <param name="Url">图片网址或IIS网址</param>
        /// <returns></returns>
        public static string DownloadPhotoFromUrl(string Url)
        {
            string strReturnFilePath = "";
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(Url);
            HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
            if (webresponse.StatusCode == HttpStatusCode.OK)
            {
                System.Drawing.Image image = System.Drawing.Image.FromStream(webresponse.GetResponseStream());
                //保存在本地文件夹
                strReturnFilePath = "\\ChaoShengData\\" + System.DateTime.Now.Year.ToString() + "\\" + System.DateTime.Now.Month.ToString() + "\\" + System.DateTime.Now.Day.ToString() + "\\";
                string strPath = @"D:" + strReturnFilePath;//在本地硬盘的绝对路径
                if (!System.IO.Directory.Exists(strPath))
                {
                    System.IO.Directory.CreateDirectory(strPath);
                }
                //image.Save(@"D:\\ChaoShengData\" + DateTime.Now.ToString("yyyyMMddHHmmssff") + ".jpg");
                image.Save(strPath + DateTime.Now.ToString("yyyyMMddHHmmssff") + ".jpg");
                //释放资源
                image.Dispose();
            }
            return strReturnFilePath;
        }

Guess you like

Origin blog.csdn.net/wybshyy/article/details/130999326