想要从网站下载图片而不是 json 文件

时间:2021-01-28 05:06:48

标签: python python-requests python-requests-html

我们需要网站的图片。但是下面的脚本下载json文件。但是我们想直接下载图片。我是初学者。提前致谢

    from threading import Timer
    import time
    import requests

    startlog = time.time()
    image_url = "https://api.data.gov.sg/v1/transport/traffic-images"


    tm = 0
    while True:
        tm += 1
        r = requests.get(image_url)  # create HTTP response object
        with open(str(tm)+"trafficFile.json", 'wb') as f:
            f.write(r.content)

   print(tm)
   time.sleep(20)

1 个答案:

答案 0 :(得分:0)

上面写的这段代码将从网上下载下面的图片。现在检查您的本地目录(此脚本所在的文件夹),您将找到此图像。