C# 爬虫

发布时间:2019-08-11编辑:佚名阅读(1859)

需要用到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# 爬虫


鼓掌

0

正能量

0

0

呵呵

0


评论区