Python中好玩的就是爬虫这块了,但是呢,我们今天不说爬虫框架,而是先了解几款开源的HTTP请求框架,好,接下来就开始我的表演了。这里我们就拿下载美女图片来演示一下

打开网易新闻 查看精彩图片

urllib3

打开网易新闻 查看精彩图片
打开网易新闻 查看精彩图片

在使用之前请先安装

pip install urllib3

打开网易新闻 查看精彩图片

其实调用步骤非常简单,就是引入urllib3包,然后请求图片地址,并保存

import osfileDir='D:/images/'if not os.path.exists(fileDir): #如果该文件夹不存在 os.mkdir(fileDir) #创建文件夹IMAGE_URL = "https://gank.io/images/882afc997ad84f8ab2a313f6ce0f3522"# 保存文件def save_pic(content,file_path): with open(file_path,'wb') as f: f.write(content)def urllib_download_img(): import urllib3 http = urllib3.PoolManager() r = http.request('GET',IMAGE_URL) save_pic(r.data,'%surllib.png ' % (fileDir))urllib_download_img()

执行该脚本,即可下载保存图片

打开网易新闻 查看精彩图片

httpx

打开网易新闻 查看精彩图片
打开网易新闻 查看精彩图片

在使用之前还是需要先安装

pip install httpx

打开网易新闻 查看精彩图片

import osfileDir = 'D:/images/'if not os.path.exists(fileDir): # 如果该文件夹不存在 os.mkdir(fileDir) # 创建文件夹IMAGE_URL = "https://gank.io/images/882afc997ad84f8ab2a313f6ce0f3522"# 保存文件def save_pic(content, file_path): with open(file_path, 'wb') as f: f.write(content)def httpx_download_img(): import httpx r = httpx.get(IMAGE_URL) save_pic(r.content, '%shttpx.png ' % (fileDir))httpx_download_img()

打开网易新闻 查看精彩图片

requests

打开网易新闻 查看精彩图片
打开网易新闻 查看精彩图片

pip install requests

打开网易新闻 查看精彩图片

这里,我们就换一张图片了,另外,我们需要添加请求头,不然有可能会被一些反爬虫的网站给拦截掉

import osfileDir = 'D:/images/'if not os.path.exists(fileDir): # 如果该文件夹不存在 os.mkdir(fileDir) # 创建文件夹IMAGE_URL = "https://gank.io/images/882afc997ad84f8ab2a313f6ce0f3522"IMAGE_URL_2 = "https://gank.io/images/02eb8ca3297f4931ab64b7ebd7b5b89c"def requests_download_img(): import requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4168.2 Safari/537.36' } r = requests.get(IMAGE_URL_2, headers=headers) save_pic(r.content, '%srequests.png ' % (fileDir))requests_download_img()

打开网易新闻 查看精彩图片

grequests

打开网易新闻 查看精彩图片
打开网易新闻 查看精彩图片

它是基于urllib3封装的异步请求库,我们先安装来试试看

pip install grequests

打开网易新闻 查看精彩图片

这个里面,我们继续换几张图片来下载

import osfileDir = 'D:/images/'if not os.path.exists(fileDir): # 如果该文件夹不存在 os.mkdir(fileDir) # 创建文件夹def grequests_download_img(): import grequests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4168.2 Safari/537.36' } urls = ['http://gank.io/images/f4f6d68bf30147e1bdd4ddbc6ad7c2a2', 'http://gank.io/images/dc75cbde1d98448183e2f9514b4d1320', 'http://gank.io/images/6b2efa591564475fb8bc32291fb0007c', 'http://gank.io/images/d6bba8cf5b8c40f9ad229844475e9149', 'http://gank.io/images/9fa43020cf724c69842eec3e13f6d21c', 'http://gank.io/images/d237f507bf1946d2b0976e581f8aab9b', 'http://gank.io/images/25d3e3db2c1248bb917c09dc4f50a46f', 'http://gank.io/images/19c99c447e0a40a6b3ff89951957cfb1', 'http://gank.io/images/f0c192e3e335400db8a709a07a891b2e', 'http://gank.io/images/bdb35e4b3c0045c799cc7a494a3db3e0'] rs = (grequests.get(u, headers=headers) for u in urls) response = grequests.map(rs) index = 0 for item in response: index = index+1 save_pic(item.content, '%s%d.png' % (fileDir, index))grequests_download_img()

打开网易新闻 查看精彩图片

最后,我把这几个例子的所有代码都汇总下,有兴趣且想学编程的朋友可以拿去玩玩

import osfileDir = 'D:/images/'if not os.path.exists(fileDir): # 如果该文件夹不存在 os.mkdir(fileDir) # 创建文件夹IMAGE_URL = "https://gank.io/images/882afc997ad84f8ab2a313f6ce0f3522"IMAGE_URL_2 = "https://gank.io/images/02eb8ca3297f4931ab64b7ebd7b5b89c"# 保存文件def save_pic(content, file_path): with open(file_path, 'wb') as f: f.write(content)def urllib_download_img(): import urllib3 http = urllib3.PoolManager() r = http.request('GET', IMAGE_URL) save_pic(r.data, '%surllib.png ' % (fileDir))def httpx_download_img(): import httpx r = httpx.get(IMAGE_URL) save_pic(r.content, '%shttpx.png ' % (fileDir))def requests_download_img(): import requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4168.2 Safari/537.36' } r = requests.get(IMAGE_URL_2, headers=headers) save_pic(r.content, '%srequests.png ' % (fileDir))def grequests_download_img(): import grequests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4168.2 Safari/537.36' } urls = ['http://gank.io/images/f4f6d68bf30147e1bdd4ddbc6ad7c2a2', 'http://gank.io/images/dc75cbde1d98448183e2f9514b4d1320', 'http://gank.io/images/6b2efa591564475fb8bc32291fb0007c', 'http://gank.io/images/d6bba8cf5b8c40f9ad229844475e9149', 'http://gank.io/images/9fa43020cf724c69842eec3e13f6d21c', 'http://gank.io/images/d237f507bf1946d2b0976e581f8aab9b', 'http://gank.io/images/25d3e3db2c1248bb917c09dc4f50a46f', 'http://gank.io/images/19c99c447e0a40a6b3ff89951957cfb1', 'http://gank.io/images/f0c192e3e335400db8a709a07a891b2e', 'http://gank.io/images/bdb35e4b3c0045c799cc7a494a3db3e0'] rs = (grequests.get(u, headers=headers) for u in urls) response = grequests.map(rs) index = 0 for item in response: index = index+1 save_pic(item.content, '%s%d.png' % (fileDir, index))grequests_download_img()

我们用得比较多的可能是requests这个库,当然,肯定还有一些其他好用的http请求库,大叔能力有限,暂时就只知道这几个。另外,这里涉及到的美女图片均来源于网络,如果涉及到版权问题请联系我删除