Skip to content

Instantly share code, notes, and snippets.

@JoshuaMa64
Created May 19, 2019 15:54
Show Gist options
  • Save JoshuaMa64/ff5d2c524d5dcdc41eb7bfcd97a6de44 to your computer and use it in GitHub Desktop.
Save JoshuaMa64/ff5d2c524d5dcdc41eb7bfcd97a6de44 to your computer and use it in GitHub Desktop.
findicons国旗图标抓取爬虫C#版
using HtmlAgilityPack;
using RestSharp;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace FlagSpider
{
class Program
{
static void Main(string[] args)
{
int count = 0;
string baseUrl = "https://findicons.com";
for (int i = 1; i < 10; i++)
{
string url = $"{baseUrl}/pack/2838/flat_round_world_flag_icon_set/{i}";
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(GetContent(url));
var query = from a in htmlDoc.DocumentNode.Descendants("a")
where a.GetAttributeValue("class", null) == "png_link downloadlink btn"
select a.Attributes["href"].Value;
foreach (var item in query)
{
Task.Factory.StartNew(async () =>
{
await Task.Delay(1500);
GetPic($"{baseUrl}{item}".Replace("128", "256"));
Console.WriteLine($"COUNT: {++count}");
});
}
}
Console.ReadLine();
}
static string GetContent(string url)
{
RestClient client = new RestClient(url);
RestRequest request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Connection", "keep-alive");
request.AddHeader("accept-encoding", "gzip, deflate");
request.AddHeader("Host", "findicons.com");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "*/*");
request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0");
IRestResponse response = client.Execute(request);
return response.Content;
}
static void GetPic(string url)
{
string fileName = $"{url.Split("/")[6]}.png";
Console.WriteLine($"Start download {fileName} ...");
RestClient client = new RestClient(url);
RestRequest request = new RestRequest(Method.GET);
byte[] response = client.DownloadData(request);
File.WriteAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "flags" ,fileName) , response);
Console.WriteLine($"Download {fileName} success.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment