pixiv批量下载器(pixivDownloader)

一个下午搞定的 当然只是个测试版 需添加引用HtmlAgilityPack 代码如下 using HtmlAgilityPack; using System; using System.Collections.Specialized; using System.Linq; using System.Net; using System.Text; namespace pixiv_loader { class Program { //作者:fuis //日期:2014.2.15 static void Main(string[] args) { Console.WriteLine("pixivDownloaderDemo written by fuis. 2014.2.15"); Console.WriteLine("请输入用户名:"); string account = Console.ReadLine(); Console.WriteLine("请输入密码:"); string password = ReadPasswd(); //模拟登录 Console.WriteLine("登录到pixiv..."); NameValueCollection loginData = new NameValueCollection(); loginData.Add(@"auth[account]", account); loginData.Add(@"auth[password]", password); loginData.Add(@"authenticity_token", "wVRrUh7mEjZFOqS9G+YCAZ5ZmRsBi687Bpy/mtLaRmw="); CookieAwareWebClient client = new CookieAwareWebClient(); client.Encoding = Encoding.UTF8; client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"); client.BaseAddress = @"http://zh.pixiv.com/"; client.UploadValues(@"/auth", "POST", loginData); //访问月排行 Console.WriteLine("获取月排行榜..."); string monthly = client.DownloadString("http://zh.pixiv.com/rankings/monthly"); string monthlyXPath = @"/html/body/div[1]/div[2]/div[1]/div[2]/ol/li"; //解析 Console.WriteLine("解析网页文件..."); HtmlDocument htmldocmonly = new HtmlDocument(); htmldocmonly.OptionFixNestedTags = true; htmldocmonly.LoadHtml(monthly); //解析错误处理 if (htmldocmonly.ParseErrors != null && htmldocmonly.ParseErrors.Count() > 0) { Console.WriteLine("解析出现错误!"); } else { HtmlNode rootNode = htmldocmonly.DocumentNode; HtmlNodeCollection nodeList = rootNode.SelectNodes(monthlyXPath); Console.WriteLine("获取到{0}张图片,开始下载", nodeList.Count); string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; for (int i = 0; i < nodeList.Count; i++) { string imageName = string.Empty; try { HtmlNode node = nodeList[i]; imageName = node.SelectSingleNode(@"div/div[1]/h2/a").InnerText; string path = node.SelectSingleNode(@"div/div[1]/h2/a").Attributes[0].Value; string large = String.Format(@"http://zh.pixiv.com{0}/large", path); HtmlDocument largeHtmlDoc = new HtmlDocument(); largeHtmlDoc.LoadHtml(client.DownloadString(large)); HtmlNode lnode = largeHtmlDoc.DocumentNode; string url = lnode.SelectSingleNode("/html/body/div[1]/a/img").Attributes[1].Value; //伪造referer client.Headers[HttpRequestHeader.Referer] = url; string extension = url.Substring(url.LastIndexOf('.')); client.DownloadFile(url, imageName + '.' + extension); Console.WriteLine(String.Format("[{2}]{0}.{1}下载完成!", imageName, extension, i)); } catch (WebException e) { Console.WriteLine(String.Format("下载{2}出现错误{0}状态:{1}", e.Message, e.Status, imageName)); } } } Console.WriteLine("所有图片下载完毕!"); Console.ReadKey(); } private static string ReadPasswd() { StringBuilder sb = new StringBuilder(); ConsoleKeyInfo k; while (true) { k = Console.ReadKey(); if (k.Key == ConsoleKey.Enter) { Console.WriteLine(); return sb.ToString(); } Console.CursorLeft--; Console.Write("*"); sb.Append(k.KeyChar); } } } public class CookieAwareWebClient : WebClient { public CookieContainer cookie = new CookieContainer(); protected override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); if (request is HttpWebRequest) { (request as HttpWebRequest).CookieContainer = cookie; } return request; } } }

你可能感兴趣的:(download)