Python 小工具下载baidu-Pic

import os

import requests
import re

num = 0
numPicture = 0
file = ''
List = []

# global num
global num1
def dowmloadPicture(html, keyword):
    global num
    # t =0
    pic_url = re.findall('"objURL":"(.*?)",', html, re.S)  # 先利用正则表达式找到图片url

    if not os.path.exists('./' + keyword):
        os.mkdir('./' + keyword)
    path = './' + keyword
    print('找到关键词:' + keyword + '的图片,即将开始下载图片...')
    for each in pic_url:
        if num < int(num1):
            print('正在下载第' + str(num + 1) + '张图片,图片地址:' + str(each))
            try:
                if each is not None:
                    pic = requests.get(each, timeout=7)
                else:
                    continue
            except BaseException:
                print('错误,当前图片无法下载')
                continue
            else:
                string = path + '_' + str(num) + '.jpg'
                with open(os.path.join(keyword,string), 'wb') as fp:
                    fp.write(pic.content)
                    fp.close()
                    num += 1

if __name__ == '__main__':  # 主函数入口
    header = {'content-type': 'application/json','User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
    word = input("请输入搜索关键词: ")

    num1 = input("请输入下载数量: ")
    # while 1:
    url = 'https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + word + '&pn='
    r = requests.get(url + num1, headers=header, allow_redirects=False)
    r.encoding = 'utf-8'
    dowmloadPicture(r.text, word)
        # num1 = num1+20

直接启动程序,输入信息

等待下载即可。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值