才开始学习几天,很简单的代码[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*
import json
import requests
import re
import os
import pysnooper
#创建文件夹
os.makedirs('./image/',exist_ok=True)
# 获取表情包图片路径
# @pysnooper.snoop('log.txt')
def get_express(text):
from urllib.request import urlretrieve
url = 'https://www.doutula.com/api/search?keyword='+text+'&mime=0&page=1'
result = requests.get(url,timeout = 30)
result.raise_for_status()
data = eval(result.content)
img_url = data['data']['list']
try:
print('共获取到 ',len(img_url),'张图片')
for i in range(len(img_url)):
image_url = img_url[i]['image_url']
if(image_url != ''):
imgUrl = image_url.replace('\\','')
print(imgUrl)
name = re.findall(r'[^/]+(?!.*/)',imgUrl)[0]
print('开始下载第',i+1,'张')
urlretrieve(imgUrl,'./image/'+name)
else:
print('图片路径为空:',image_url)
except IOError:
print('下载图片出错!')
text = input('请输入下载表情关键字:')
get_express(text) |