发布时间:2019-08-11编辑:佚名阅读(1998)
需要用到html解析插件 -工具- NuGet程序包 <jumony core>
string url = "https://www.suajin.com/"; void Index() { IHtmlDocument source = new JumonyParser().LoadDocument(url,System.Text.Encoding.GetEncoding("utf-8")); var links = source.Find("img"); int num = 0; foreach (var link in links) { string value = link.Attribute("data-src").Value(); if (value != null) { num++; string path = desPath + "\\" + num; DownloadImage2(value, path+".gif"); } } } string desPath = "C:\\gif"; private void DownloadImage2(string objUrl, string destFileName) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(objUrl); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode == HttpStatusCode.OK) { using (Stream stream = response.GetResponseStream()) { using (FileStream fileStream = new FileStream(destFileName, FileMode.Create)) { stream.CopyTo(fileStream);//stream 写入到fileStream中 } } } else { throw new Exception("下载" + objUrl + "失败,错误码:" + response.StatusCode); } } }
上一篇:一个简单c#爬虫程序
下一篇:c#保存txt
0人
0人
0人
0人