代码之家  ›  专栏  ›  技术社区  ›  mmewtwosaysbye

如何在python中更改变量的输出?

  •  0
  • mmewtwosaysbye  · 技术社区  · 2 年前

    我当前的代码是

    #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吗?

    1 回复  |  直到 2 年前
        1
  •  1
  •   OregonTrail    2 年前
    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)
    
    # Code
    print(filename)