男人的快乐很简单!
每天看看段子,看看美女,上上班,喝喝茶就够了!
今天教大家使用API获取段子和美图,每次载入获取不同的随机内容!
下面直接上代码(代码才是快乐,哈哈):
#函数1:按比例缩放图片(不然图片充满整个屏幕)
def resize(w, h, w_box, h_box, pil_image):
f1 = 1.0*w_box/w
f2 = 1.0*h_box/h
factor = min([f1, f2])
width = int(w*factor)
height = int(h*factor)
return pil_image.resize((width, height), Image.Resampling.LANCZOS)
#函数2:API获取美图并保存(想看原图,可直接打开代码根目录,k.jpg就是你想要的)
def img_get():
res=requests.get('https://www.yuanxiapi.cn/api/img/?type=meinv&format=json')
jsonData = json.loads(res.content.decode('utf-8'))
os.makedirs('./image/', exist_ok=True)
urlretrieve(jsonData['imgurl'], './image/k.jpg')
return jsonData['imgurl']
img=img_get()
#函数3:API获取段子并返回正文
def date_get():
res=requests.get('https://yuanxiapi.cn/api/Aword/')
jsonData = json.loads(res.content