@Blog
過去の蓄積を見るために書く日記.
逃げるは恥だが役に立つグーグル画像検索ダウンロードコンソールアプリ
文字数[5196文字] この記事は6分30秒で読めます.

グーグル画像検索ダウンロードコンソールアプリを作ってみました。
へっぽこソースはこちら
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
namespace google_img_get
{
class Program
{
static void Main(string[] args)
{
gurl();
}
static void gurl() {
string url = "https://www.google.co.jp/search?tbm=isch&biw=1920&bih=957";
string htmlSource = "";
Boolean flg = true;
try
{
//コマンドライン引数を配列で取得する
string[] cmds = System.Environment.GetCommandLineArgs();
if (cmds.Length == 2)
{
url = url + "&q=" + cmds[1] + "&oq=" + cmds[1];
flg = false;
}
if (flg)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("***** google画像検索&ダウンロード少しw キーワードを入力してください *****");
Console.ForegroundColor = ConsoleColor.White;
string hoge= Console.ReadLine();
url = url + "&q=" + hoge + "&oq=" + hoge;
}
if (url != "")
{
Console.WriteLine("***** google check *****");
htmlSource = gsource(url);
Console.WriteLine(htmlSource);
string img = "\"ou\":\"(?<text>https?.*?)\"";
Regex reimg = new Regex(img, RegexOptions.IgnoreCase | RegexOptions.Singleline);
for (Match m = reimg.Match(htmlSource); m.Success; m = m.NextMatch())
{
char[] cc = { '"', '\'' };
string u = m.Groups["text"].Value;
u = u.Trim(cc);
string[] exte = u.Split('.');
string gexte = "";
if (Regex.IsMatch(exte[exte.Length - 1].ToLowerInvariant(),"jpge")) {
gexte = "jpg";
}
if (Regex.IsMatch(exte[exte.Length - 1].ToLowerInvariant(), "jpg"))
{
gexte = "jpg";
}
if (Regex.IsMatch(exte[exte.Length - 1].ToLowerInvariant(), "gif"))
{
gexte = "gif";
}
if (Regex.IsMatch(exte[exte.Length - 1].ToLowerInvariant(), "png"))
{
gexte = "png";
}
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("***** " + u + " *****");
if (gexte != "")
{
int ihoge = new Random(100).Next(100) * 100;
Console.WriteLine(media(u, gexte));
System.Threading.Thread.Sleep(ihoge);
}
}
}
gurl();
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("***** " + e.Message + " *****");
gurl();
}
}
static string gsource(string url) {
string htmlSource = "";
WebClient client = new WebClient();
client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
Stream st = client.OpenRead(url);
Encoding enc = Encoding.GetEncoding("UTF-8");
StreamReader sr = new StreamReader(st, enc);
htmlSource = sr.ReadToEnd();
sr.Close();
st.Close();
client.Dispose();
return htmlSource;
}
static string media(string url, string exte)
{
// カレントディレクトリを取得する
string nowstr = DateTime.Now.ToString("yyyyMMddHHmmss");
string stCurrentDir = Directory.GetCurrentDirectory();
WebClient wc = new WebClient();
wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
@wc.DownloadFile(url, @stCurrentDir + "\\webcmd" + nowstr + "." + exte);
wc.Dispose();
return "***** " + "Success!! [webcmd" + nowstr + "." + exte + "] *****";
}
}
}
コンソールアプリ軽量で動作が機敏なので良いかなと
これを使用していているとリモートサーバーから怒られることが
あるのでお気をつけください。リモートサーバー=プロバイダと認識しています。
あまりにも立て続けにダウンロードしていると400エラーになり
処理が途中で中断してしまいます。
何故、そうなるのかはお察しください。
https://www.youtube.com/watch?v=_CuVYooExZ4
回避策のため、ランダムタイマーで一旦処理を停止しながら
ダウンロードを行っているので微妙に遅い。
これを改良してもっとより良いものを
作ってくれる有志が入れば有り難いです、GitHubにも
公開していますので改良してもらうと有り難いです。
その他、コンソールアプリの詰め合わせzipを置いときますので
ご自由にご使用いただければと思っています。
法的にまずい使い方は行わないでください。
https://zip358.com/tool/web.zip
1227番目の投稿です/228 回表示されています.
中の人🏠️
AIによるおすすめ記事
著者名
@taoka_toshiaki
※この記事は著者が30代後半に書いたものです.
Profile
高知県在住の@taoka_toshiakiです、記事を読んで頂きありがとうございます.
数十年前から息を吸うように日々記事を書いてます.たまに休んだりする日もありますがほぼ毎日投稿を心掛けています😅.
SNSも使っています、フォロー、いいね、シェア宜しくお願い致します🙇.
SNS::@taoka_toshiaki
タグ
コメントを残す