pythonで画像ダウンロードしたいなら。コレでよし🤔

2022.08.06

Logging

こんにちは、日が暮れて夕方になってしまいましたね?…本日の更新です😌。

今日は機械学習(tensorflow)の為の画像を集めをしていたました。画像を集めるのが面倒くさくて昔、ダウンロード用のソフトを自前で作っていたのだけど、その自前のソフトをいつの間にか消し去っていた為、再度Pythonで作り直しました。

作ったのですが、これは即席なダウンロードソフトなので完璧なものではないです。50枚~300枚の画像をダウンロード出来ます。そのダウンロードした画像を水回しして機械学習の画像分類に使用しているのですが、学習精度があまり良くないのが明日の課題。

人工知能に学習させる方法にはいろいろな方法があります。それらを上手く使えばもっと効率よく学習出来るみたいですが、そもそもPythonも普段から使用しないので手探り状態です。

もっと綺麗なコードを書けると思いますが…。

トイウコトデ、コードを記載しときますね、このコードはbingサイトで画像ダウンロードするために作られたものです。機械学習しているコードではありません。

from functools import cache
import time
import requests
import os
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
options = Options()
# download_path = 'C:\python\images\face'
# options.add_experimental_option("prefs", {"download.default_directory": download_path})
# driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.bing.com/images/search?q=顔&form=HDRSC2&first=1&tsc=ImageHoverTitle")
driver.set_window_size(945, 1012)
time.sleep(3)
sl = 700
for i in range(30):
    driver.execute_script("window.scrollTo(0," + str(sl) + ")")
    time.sleep(3)
    sl = sl + 700
img = []
for x in range(10):
    for y in range(100):
        try:
            txt = driver.find_element(By.XPATH,"//*[@id=\"mmComponent_images_2\"]/ul[" + str(int(y +1 )) +"]/li[" + str(int(x +1 )) +"]/div/div[1]/a").get_attribute("m")
            hoge = str(txt).split(",")
            #print(hoge)
            img.append([s for s in hoge if "murl" in s])
        except:
            print("errors not image")
driver.quit()
file_dir = "C:\\python\\images\\face\\"
for imgdata in img:
    url = str(imgdata).split(":")[1] + ":" + str(imgdata).split(":")[2]
    url = url.replace('"',"").replace("']","")
    print(url)
    try:
        urlData = requests.get(url).content
        with open(os.path.join(file_dir,os.path.basename(url)),'wb') as f:
            f.write(urlData)
    except:
        print("errors not Download")

著者名  @taoka_toshiaki

※この記事は著者が40代前半に書いたものです.

Profile
高知県在住の@taoka_toshiakiです、記事を読んで頂きありがとうございます.
数十年前から息を吸うように日々記事を書いてます.たまに休んだりする日もありますがほぼ毎日投稿を心掛けています😅.
SNSも使っています、フォロー、いいね、シェア宜しくお願い致します🙇.
SNS::@taoka_toshiaki

OFUSEで応援を送る

タグ

ChromeDriverManager, driver, driver.get, driver.quit, except, img.append, imgdata, options, print, quot, replace, scrollTo, selenium.webdriver.support.wait, sl, sleep, tensorflow, time.sleep, ul, urlData, トイウコトデ,

Gさんのリアルタイム検索トレンドを抽出するPHPライブラリ

2020.04.15

Logging

Composerをインストールしていること前提条件として書いていきます。まずはライブラリーを入れます。

こんな感じに

composer require x-fran/g-trends

そして次にこんな感じのPHPコードを書きます、これだけでGさん(グーグルさん)のリアルタイム検索トレンドが表示することが可能となります。ちなみにJSONで返す処理として書いています。ライブラリーがしっかりしているので、あとはメソッドを呼び出すだけで簡単に検索のリアルタイムのトレンドが表示できてしまう。

ライブラリーコードは下記から参照できます。
https://github.com/x-fran/g-trends

<?php
include_once "../../vendor/autoload.php";
use XFran\GTrends\GTrends;
$options = [
    'hl' => 'ja-JP',
    'tz' => -540,
    'geo' => 'JP',
];
if((int)$_POST["p"]==1234){
    $gt =  new GTrends($options);
    $ret = $gt->getRealTimeSearchTrends();
    if(is_array($ret["storySummaries"]["trendingStories"])){
        $jsn = json_encode($ret["storySummaries"]["trendingStories"]);
        print $jsn;
    }
}

尚、ajaxで表示している部分は割愛します。
https://zip358.com/tool/demo14/ demo14を検証ください。

著者名  @taoka_toshiaki

※この記事は著者が40代前半に書いたものです.

Profile
高知県在住の@taoka_toshiakiです、記事を読んで頂きありがとうございます.
数十年前から息を吸うように日々記事を書いてます.たまに休んだりする日もありますがほぼ毎日投稿を心掛けています😅.
SNSも使っています、フォロー、いいね、シェア宜しくお願い致します🙇.
SNS::@taoka_toshiaki

OFUSEで応援を送る

タグ

autoload, com, Composer, g-trends, github, GTrends, https, include, json, lt, once, options, php, quot, require, use, vendor, x-fran, XFran, あと, インストール, グーグル, コード, こと, これだけ, トレンド, メソッド, ライブラリ, ライブラリー, リアルタイム, 下記, 処理, 前提, 参照, 可能, 感じ, 抽出, 条件, 検索, 簡単, 表示,

jQuery独自プラグイン基本サンプル2

2019.01.19

Logging

忘れやすい自分のためにメモとして残しときます。

(function($){
    $.fn.smp = function (options) {
        var config = {
            text:"おいら"
        };
        var cfg = $.extend({}, config, options);
        cfg.text = cfg.text + "のお金";
        return $(this).html(cfg.text);
    };
})(jQuery);
$("#hoge1").smp({text:"ぼく"});
$("#hoge2").smp();

著者名  @taoka_toshiaki

※この記事は著者が30代前半に書いたものです.

Profile
高知県在住の@taoka_toshiakiです、記事を読んで頂きありがとうございます.
数十年前から息を吸うように日々記事を書いてます.たまに休んだりする日もありますがほぼ毎日投稿を心掛けています😅.
SNSも使っています、フォロー、いいね、シェア宜しくお願い致します🙇.
SNS::@taoka_toshiaki

OFUSEで応援を送る

タグ

, 2, cfg, config, extend, fn, function, hoge, html, jquery, options, return, smp, Text, this, var, おいら, お金, サンプル, ため, プラグイン, ぼく, メモ, 基本, 自分,