如何使用Python自动爬取图片并保存(python,开发技术)

时间:2024-05-10 06:30:12 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

一、准备工作

用python来实现对百度图片的爬取并保存,以情绪图片为例,百度搜索可得到下图所示

如何使用Python自动爬取图片并保存

f12打开源码

如何使用Python自动爬取图片并保存

在此处可以看到这次我们要爬取的图片的基本信息是在img - scr中

二、代码实现

这次的爬取主要用了如下的第三方库

importreimporttimeimportrequestsfrombs4importBeautifulSoupimportos

简单构思可以分为三个小部分

1.获取网页内容

2.解析网页

3.保存图片至相应位置

下面来看第一部分:获取网页内容

baseurl='https://cn.bing.com/images/search?q=%E6%83%85%E7%BB%AA%E5%9B%BE%E7%89%87&qpvt=%e6%83%85%e7%bb%aa%e5%9b%be%e7%89%87&form=IGRE&first=1&cw=418&ch=652&tsc=ImageBasicHover'head={"User-Agent":"Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/92.0.4515.131Safari/537.36Edg/92.0.902.67"}response=requests.get(baseurl,headers=head)#获取网页信息html=response.text#将网页信息转化为text形式

是不是so easy

第二部分解析网页才是大头

来看代码

Img=re.compile(r'img.*src="(.*?)"')#正则表达式匹配图片soup=BeautifulSoup(html,"html.parser")#BeautifulSoup解析html#i=0#计数器初始值data=[]#存储图片超链接的列表foriteminsoup.find_all('img',src=""):#soup.find_all对网页中的img—src进行迭代item=str(item)#转换为str类型Picture=re.findall(Img,item)#结合re正则表达式和BeautifulSoup,仅返回超链接forbinPicture:data.append(b)#i=i+1returndata[-1]#print(i)

这里就运用到了BeautifulSoup以及re正则表达式的相关知识,需要有一定的基础哦

下面就是第三部分:保存图片

formingetdata(baseurl='https://cn.bing.com/images/search?q=%E6%83%85%E7%BB%AA%E5%9B%BE%E7%89%87&qpvt=%e6%83%85%e7%bb%aa%e5%9b%be%e7%89%87&form=IGRE&first=1&cw=418&ch=652&tsc=ImageBasicHover'):resp=requests.get(m)#获取网页信息byte=resp.content#转化为content二进制print(os.getcwd())#os库中输出当前的路径i=i+1#递增#img_path=os.path.join(m)withopen("path{}.jpg".format(i),"wb")asf:#文件写入f.write(byte)time.sleep(0.5)#每隔0.5秒下载一张图片放入D://情绪图片测试print("第{}张图片爬取成功!".format(i))

各行代码的解释已经给大家写在注释中啦,不明白的地方可以直接私信或评论哦~

下面是完整的代码

importreimporttimeimportrequestsfrombs4importBeautifulSoupimportos#m='https://tse2-mm.cn.bing.net/th/id/OIP-C.uihwmxDdgfK4FlCIXx-3jgHaPc?w=115&h=183&c=7&r=0&o=5&pid=1.7''''resp=requests.get(m)byte=resp.contentprint(os.getcwd())img_path=os.path.join(m)'''defmain():baseurl='https://cn.bing.com/images/search?q=%E6%83%85%E7%BB%AA%E5%9B%BE%E7%89%87&qpvt=%e6%83%85%e7%bb%aa%e5%9b%be%e7%89%87&form=IGRE&first=1&cw=418&ch=652&tsc=ImageBasicHover'datalist=getdata(baseurl)defgetdata(baseurl):Img=re.compile(r'img.*src="(.*?)"')#正则表达式匹配图片datalist=[]head={"User-Agent":"Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/92.0.4515.131Safari/537.36Edg/92.0.902.67"}response=requests.get(baseurl,headers=head)#获取网页信息html=response.text#将网页信息转化为text形式soup=BeautifulSoup(html,"html.parser")#BeautifulSoup解析html#i=0#计数器初始值data=[]#存储图片超链接的列表foriteminsoup.find_all('img',src=""):#soup.find_all对网页中的img—src进行迭代item=str(item)#转换为str类型Picture=re.findall(Img,item)#结合re正则表达式和BeautifulSoup,仅返回超链接forbinPicture:#遍历列表,取最后一次结果data.append(b)#i=i+1datalist.append(data[-1])returndatalist#返回一个包含超链接的新列表#print(i)'''withopen("img_path.jpg","wb")asf:f.write(byte)'''if__name__=='__main__':os.chdir("D://情绪图片测试")main()i=0#图片名递增formingetdata(baseurl='https://cn.bing.com/images/search?q=%E6%83%85%E7%BB%AA%E5%9B%BE%E7%89%87&qpvt=%e6%83%85%e7%bb%aa%e5%9b%be%e7%89%87&form=IGRE&first=1&cw=418&ch=652&tsc=ImageBasicHover'):resp=requests.get(m)#获取网页信息byte=resp.content#转化为content二进制print(os.getcwd())#os库中输出当前的路径i=i+1#递增#img_path=os.path.join(m)withopen("path{}.jpg".format(i),"wb")asf:#文件写入f.write(byte)time.sleep(0.5)#每隔0.5秒下载一张图片放入D://情绪图片测试print("第{}张图片爬取成功!".format(i))

最后的运行截图

如何使用Python自动爬取图片并保存

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:如何使用Python自动爬取图片并保存的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Java中classpath怎么用下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18