Skip to content

Instantly share code, notes, and snippets.

@nbuss848
Created April 30, 2019 03:33
Show Gist options
  • Save nbuss848/627ac6f49b39c924dc5cdbd5000f8e4d to your computer and use it in GitHub Desktop.
Save nbuss848/627ac6f49b39c924dc5cdbd5000f8e4d to your computer and use it in GitHub Desktop.
This C# snippet demonstrates how you can potentially scrape images and then save them locally
public static string Path = @"C:\Users\USERNAME\Pictures\Wallpapers\";
public static string Download()
{
string randomImageLink = String.Empty;
string url = "https://alpha.wallhaven.cc/search?q=nature&categories=111&purity=100&resolutions=1920x1080&sorting=random&order=desc";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.CookieContainer = new CookieContainer();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
WebHeaderCollection header = response.Headers;
string responseText = String.Empty;
var encoding = ASCIIEncoding.ASCII;
using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
{
responseText = reader.ReadToEnd();
}
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(responseText);
var children = doc.DocumentNode.SelectNodes("//img");
foreach(var i in children)
{
if (i.Attributes.Count > 2)
{
//So that we can format our image URL
if (!i.Attributes.Contains("data-src")) continue;
string [] arr = i.Attributes["data-src"].Value.Split(new string[] { "th-" },StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(arr[1]);
randomImageLink = "https://alpha.wallhaven.cc/wallpapers/full/wallhaven-" + arr[1];
DownloadImage(randomImageLink);
Console.WriteLine(randomImageLink);
}
}
return randomImageLink;
}
public static void DownloadImage(string url)
{
using (WebClient client = new WebClient())
{
string [] imagename = url.Split(new string[] { "https://alpha.wallhaven.cc/wallpapers/full/" },
StringSplitOptions.RemoveEmptyEntries);
try
{
client.DownloadFile(new Uri(url), Path + imagename[0]);
}
catch
{
try
{
client.DownloadFile(new Uri(url.Replace(".jpg", ".png")), Path + imagename[0].Replace(".jpg", ".png"));
}
catch
{
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment