使用Python下载一张图片

1、下载requests:打开Anaconda prompt,输入pip install requests下载

2、验证是否下载成功:

  • 输入python
  • 输入import requests,如果没有错误提示信息,则表示下载成功,可以使用

3、下载图片

import requests

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"}
url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1579097271944&di=4608f71a16536284384b484a318eab5c&imgtype=0&src=http%3A%2F%2Fm.360buyimg.com%2Fmobilecms%2Fs842x929_jfs%2Ft5758%2F164%2F2979156342%2F394913%2F2bdd991e%2F59362610N05ae6b75.jpg%2521q70.jpg"

#发送get请求
reponse = requests.get(url,headers = headers)

#变为byte流数据
content = reponse.content

print(content)

#保存图片
with open('cat.jpg','wb') as f:
    f.write(content)

 

你可能感兴趣的:(python)