爬虫学习,图片的两种下载方法

import requests
import urllib

headers ={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'
}

img_url = 'https://t8.baidu.com/it/u=1484500186,1503043093&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1590815730&t=3fa45038ddfa8b1df8fd976381290391'

#方式一
response = requests.get(url=img_url, headers= headers)
img_data = response.content  #content返回的是二进制形式的响应数据
with open('1.jpg', 'wb') as fp:
    fp.write(img_data)

#方式二

urllib.request.urlretrieve(img_url, './2.jpg')

你可能感兴趣的:(笔记)