Python爬取百度图片—模板

版权声明:如有帮助,赞一个可好。邮箱:[email protected] https://blog.csdn.net/qq_40946921/article/details/89301592

 需要第三方库requests支持

         通过pip安装

         cmd输入:pip install requests

通过关键字进行爬取

import json
import os
import requests
def downloadImage(keyword,begin,end,file):
    id=0;
    if not os.path.exists(file):  # 目录不存在时,创建目录
        os.makedirs(file)
    for i in range(begin*30,end*30,30):
        data   = ({
            'tn': 'resultjson_com',
            'ipn': 'rj',         
            'queryWord': keyword,
            'word': keyword,     
            'pn': i,
            'rn': 30    
         })
        url = 'https://image.baidu.com/search/acjson'
        req=requests.get(url,params=data).json().get("data");
        for it in req:
            if 'thumbURL' in it and len(it['thumbURL'])>0:
                 print('正在下载:%s' % it['thumbURL'])
                 img = requests.get(it['thumbURL'])
                 open(file + '%d.jpg' % id, 'wb').write(img.content)
                 id += 1
            else:
                 print('链接已失效')
if __name__ == '__main__':
    dataList = downloadImage('星空',0,100,'d:/myImage/')  # (关键字,起始页数,终止页数(30张/页),存储目录)

猜你喜欢

转载自blog.csdn.net/qq_40946921/article/details/89301592