bingから画像を一括ダウンロードするアプリ。

20200614

Logging

任意のキーワード検索でbing画像からを一括ダウンロードするアプリを作りました。img-dl-bing-sss.exeファイルを起動すると動画のようなコンソール画面が開くので入力してダウンロードを開始してください。ダウンロードが終わると勝手に閉じます。

なお、一応R指定にも対応しています、キーワードがR指定項目かもと思う場合、Yと入力ください。
新垣結衣さんの熱烈なファンでもないですが、綺麗な方だとは思います。ということで例として使用させて頂きました、、、関係者の皆様ごめんなさい。

bingから画像を一括ダウンロードするアプリ

このアプリはウィンドウズ10環境で動作します。また、動作しないブロックされる場合は下記のURL等を参照しブロックを解除しお使いください。ブロック解除方法:https://forest.watch.impress.co.jp/docs/news/1130655.html

※なお、Bingサイトのデザインが変わった場合、動作しなくなりますのであしからず。

画像ダウンロードアプリのソースコードはこちら(古いコードですので動かない場合がありますVS2019C#)

using System;
using System.Runtime.CompilerServices;

namespace img_dl_bing_sss
{
    class Program
    {
        static void Main(string[] args)
        {
            string keyword = "",R18="";
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("処理中は画面や立ち上がったブラウザを閉じないでください。\n" +
                "処理が完了すると「***終了します***」と表示されます。\n" +
                "何かキーを押し終了してください\n\n");


            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("↓検索キーワードを入力しエンターを押してください");
            keyword = Console.ReadLine();
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("↓R指定ですか?\nY or N かの文字を入力しエンターを押してください");
            R18 = Console.ReadLine();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("***画像のダウンロード量(画像数ではありません) 1?999 ***");
            var scroll = 9999;
            try
            {
                scroll = int.Parse(Console.ReadLine());
                if(scroll >=1 && scroll <= 999)
                {
                    scroll = scroll * 9999;
                }
            }
            catch
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("***検索キーワードが不適切です。***");
            }
            SuiteTests img =  new SuiteTests();
            if (img.Imgdlok(keyword, R18, scroll))
            {
                img.Dispose();
            }
            else {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("***検索キーワードが不適切です。***");
            }
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("***終了します***");
            
        }
    }
}
// Generated by Selenium IDE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using Xunit;
using AngleSharp;
using AngleSharp.Html.Parser;
using AngleSharp.Html.Dom;
using AngleSharp.Dom;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using System.IO;
using System.Net;

public class SuiteTests : IDisposable {
  public IWebDriver driver {get; private set;}
  public IDictionary<String, Object> vars {get; private set;}
  public IJavaScriptExecutor js {get; private set;}
  public SuiteTests()
  {
    driver = new ChromeDriver();
    js = (IJavaScriptExecutor)driver;
    vars = new Dictionary<String, Object>();
  }
  public void Dispose()
  {
    driver.Quit();
  }
  public bool Imgdlok(string keyword="", string R18= "",int scroll = 999999)
    {
        if (keyword == "") return false;
        Console.Clear();
        string path = System.IO.Directory.GetCurrentDirectory() + "\\IMG\\";
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        driver.Navigate().GoToUrl("https://www.bing.com/?scope=images&nr=1&FORM=NOFORM");
        driver.Manage().Window.Size = new System.Drawing.Size(945, 1030);
        driver.FindElement(By.Id("sb_form_q")).Click();
        driver.FindElement(By.Id("sb_form_q")).SendKeys(keyword);
        driver.FindElement(By.CssSelector(".search.icon.tooltip")).Click();
        try
        {
            if (R18 == "Y")
        {
            driver.FindElement(By.LinkText("設定の変更")).Click();
            driver.FindElement(By.CssSelector("#settings_safesearch div:nth-child(3)")).Click();
            driver.FindElement(By.Id("adlt_set_off")).Click();
            driver.FindElement(By.Id("sv_btn")).Click();
            driver.FindElement(By.Id("adlt_confirm")).Click();
        }
        }
        catch
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("***R指定ではありません***");
        }
        try
        {
            double ps = 0;
                for (int i = 99; i < scroll;i+=1000)
            {

                js.ExecuteScript("window.scrollTo(0," + i + ")");
                Console.CursorLeft = 0;
                ps = (double)(((double)i / (double)scroll) * 100);
                Console.Write("処理中::{0:D2}%",(int)ps);
            }
        }
        catch
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("***Max画像まで到達しました***");
        }
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("\n***ダウンロード処理開始***\n");
        int cnt = 0;
        var elm = driver.FindElements(By.ClassName("iusc"));
        driver.Manage().Window.Minimize();
        foreach (var e in elm) {
           dynamic jsondata = JsonConvert.DeserializeObject(e.GetAttribute("m"));
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine((string)jsondata.murl + "::wait.....");
            string imgurl = (string)jsondata.murl;
            if(4 <= Path.GetExtension(imgurl).Length && Path.GetExtension(imgurl).Length <= 5)
            {
                try
                {
                    cnt++;
                    string ext = Path.GetExtension(imgurl);
                    WebClient myWebClient = new WebClient();
                    myWebClient.DownloadFile(imgurl, path + "No-" + cnt + "-" + keyword + ext);
                    myWebClient.Dispose();
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine("***" + cnt +"::" + imgurl + "\n::ダウンロード中、画面を閉じないでください...***");
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("***" + cnt + "::ダウンロード済***");
                }
                catch
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("***ダウンロードが失敗しました***");
                }

            }else{
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("***画像の拡張子が不明のためダウンロードが失敗しました***");
            }
        }
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("***ダウンロード完了しました***\n" + path);
        return true;
  }
}

コメントは受け付けていません。

タグ

10, bing, exe, img-dl-bing-sss, url, VS, アプリ, ウィンドウズ, キーワード, コード, こちら, こと, コンソール, サイト, ソース, ダウンロード, デザイン, ファイル, ファン, ブロック, 一括, 下記, 任意, 使用, , 入力, 動作, 動画, 勝手, 参照, 場合, 対応, 指定, 新垣結衣, , 方法, 検索, 環境, 画像, 画面, 皆様, , 綺麗, 解除, 起動, 開始, 関係者, 項目,