抓取随机图片

最近收集了一些随机图片网址,想把这些图片全部抓取下来,到网上搜了搜,都只有抓取链接的,没有抓取图片的,这时正好看到了VariousArtist抓图自动机,赶紧下了个Python(我是C++党)来试试,结果发现……

这份代码实在是太!鸡!肋!了!,运行一次只能抓一张图片,如果靠人工点的话岂不是要点到地老天荒,于是,我编写了一个.bat程序和一个C++程序,用来配合那份Python代码,一次抓取多张图片。

具体步骤如下:

1.配置好Python和C++废话!

2.在E盘中新建一个文件夹Photo(也可以不放在E盘里,但是后面需要根据自己存放Photo文件夹的位置稍微改变一下(建议放在空间大的磁盘里,不然抓取图片抓到一半内存不够就尴尬了))

3.在Photo文件夹里新建一个photo.txt文件,在里面写上如下代码:

from urllib import request
import os
import time
import hashlib
linkn = 'https://api.3ewl.cc/acg/img.php'
path = os.getcwd()
print ('Current Work Path is:',path)
ftcnt = 0

def filecount():
    filecount = int(os.popen('dir /B |find /V /C ""').read())
    return (filecount)

def md5sum(filename):
    f = open(filename, 'rb')
    md5 = hashlib.md5()
    while True:
        fb = f.read(8096)
        if not fb:
            break
        md5.update(fb)
    f.close()
    return (md5.hexdigest())

def delfile():
    all_md5 = {}
    filedir = os.walk(os.getcwd())
    for i in filedir:
        for tlie in i[2]:
            if md5sum(tlie) in all_md5.values():
                os.remove(tlie)
            else:
                all_md5[tlie] = md5sum(tlie)

oldf = 0
while True:
    if (oldf + ftcnt) % 100 == 0:
        print('Cleaning Double Files.')
        oldf = filecount()
        print(oldf, 'Files Before Removal.')
        delfile()
        print(filecount(), 'Files After Removal.')
        print('Deleted ', oldf - filecount(), 'Files.')
        oldf = filecount()

    ftcnt = ftcnt + 1
    if ftcnt % 40 == 0:
        print('Fetching',ftcnt,'th Picture from ',linkn,'...')
    request.urlretrieve(linkn,path + '\\' + str(time.time()) + '.jpg')

其中第五行的https://api.3ewl.cc/acg/img.php是一个随机图片链接,可以替换为其他的随机图片链接。

编写好之后,将这个文件重命名为photo.pyw

4.在Photo文件夹里新建一个文件bat.txt,在里面写下如下代码:

start E:\Photo\photo.pyw

(如果你的Photo文件夹没放在E盘里,请自行修改代码!)

编写好之后,将这个文件重命名为bat.bat

5.在Photo文件夹里新建一个文件cpp.cpp,在里面写下如下代码:

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    int t;
    cout<<"请输入你要抓取的次数:";
    cin>>t;
    for(int i=1;i<=t;i++)
        system("cmd.exe /c \"E:\\Photo\\bat.bat\"");
    return 0;
}

(如果你的Photo文件夹没放在E盘里,请自行修改代码system("cmd.exe /c \"E:\\Photo\\bat.bat\"");!)

编写好之后,编译运行这个C++代码,然后会提示你输入次数,输入后回车确定,接下来就是耐心等待程序运行完毕,图片就抓取完成啦!

程序运行完毕后,打开Photo文件夹,你就会发现,有许多图片静静地躺在这个文件夹里啦!

(因为这个程序有去重功能,所以抓取的越多越慢,建议一次抓取\(100\)~\(300\)次左右,当然,如果你家电脑配置足够好,那都随便啦!)

最后附上我收集的随机图片网址:

https://acg.toubiec.cn/random
http://jump2.bdimg.com/safecheck/index?url=rN3wPs8te/pL4AOY0zAwhwPDt1WAsCMMTkZGmy7i0bP6XsqIg2Wl6PHQ2HhF3NZelyIdz9v+qXV+GAYqxfjnDPXCnfW1ESuj6A3tY9na+QlFB65G9e67GL0F1G10srGPylUecCg3xQhV92TAmoWV0zA8Zu4mdgY0
http://www.dmoe.cc/random.php
http://api.apizz.cn/ecyt/api.php
https://yesos.cn/api/acgurl.php
https://api.wuzuhua.cn/dm
https://acg.yanwz.cn/acg.php
https://acg.yanwz.cn/menhera/api.php
https://acg.yanwz.cn/api.php
https://www.xiaobaibk.com/api/acg
https://www.niubai.net/yanshi/acgapi/acg.php
http://api.mtyqx.cn/api/random.php
http://api.mtyqx.cn/tapi/random.php
https://www.xwboke.cn/api/api.php
https://yun.iqinxiu.com/moe/a/
https://yun.iqinxiu.com/moe/b/
https://yun.iqinxiu.com/moe/c/
https://yun.iqinxiu.com/moe/d/

猜你喜欢

转载自www.cnblogs.com/Naive-Cat/p/10664013.html