如何更改Python中变量的输出?

问题描述 投票:0回答:1

我当前的代码是

#imports
import requests
#setup
response = requests.get("https://randomfox.ca/floof/")
fox = response.json()
#code
print(fox['image'])

每当我运行它时,它总是打印类似 https://randomfox.ca/images/99.jpg 的内容。 有什么办法可以改变它并使其变成 99.jpg 吗?

python variables
1个回答
0
投票
import requests
import os

# Setup
response = requests.get("https://randomfox.ca/floof/")
fox = response.json()

# Extract the filename
image_url = fox['image']
filename = os.path.basename(image_url)

# Codeprint(filename)
© www.soinside.com 2019 - 2024. All rights reserved.