python爬虫爬取好看的图片
来源:互联网 发布:乐视1s网络 编辑:程序博客网 时间:2024/05/16 23:57
遇到好看的图片你还在一张张用鼠标点击保存?作为一个IT从业者那就out了。。。必须高大上起来
今天给大家介绍如何用爬虫爬取好看的图片,话不多说,开始。。。。。
首先小伙伴们需要确保自己已经安装了python和pip,并且已经配置好了环境变量,接下来需要安装今天所用到的第三方库,requests和BeautifulSoup(这是已经安装好的,大家可以使用pip install命令进行安装)
安装完成后就开始我们今天的爬虫过程,先上代码
#-*- coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup
def get_html(urllist):
res = requests.get(urllist)
soup = BeautifulSoup(res.text, 'html.parser')
src = soup.find_all("img",class_="BDE_Image")
return src
def write_img(url):
for link in url:
picurl = link.get('src')
picres = requests.get(picurl).content
file = open(r"C:\images\%s.jpg"%picurl[-9:-4],"wb")
file.write(picres)
file.close()
url = get_html("https://tieba.baidu.com/p/5254036393")
write_img(url)
最后爬出的效果如下
下面一行一行解释一下实现的过程
import requests#导入requests模块
from bs4 import BeautifulSoup#导入BeautifulSoup模块
def get_html(urllist):
res = requests.get(urllist)#获取网页请求
soup = BeautifulSoup(res.text, 'html.parser')#将网页转化为soup对象标准xml输出
src = soup.find_all("img",class_="BDE_Image")#找到所有img标签,并且class=BDE_Image的项
return src#返回image对象集合
def write_img(url):
for link in url:
picurl = link.get('src')#获取image的url
picres = requests.get(picurl).content#获取image二进制流数据
file = open(r"C:\images\%s.jpg"%picurl[-9:-4],"wb")#创建图片
file.write(picres)#写入
file.close()#关闭
过程中最主要的是如何找到image的url,首先打开需要下载图片的网页,按F12打开开发者工具,然后在网页源代码中查找image的地址
从网页源代码中可以看出,所以得图片都具有某一种共性,如都是img标签且class都为BDE_Image,因此可以BeautifulSoup的方法或者正则表达式提取出所有的image地址,然后将图片保存下来。
当然爬取图片的方法有很多种比如使用urllib2库等,各位小伙伴可以根据实际情况挑选一种适合自己的方法,接下来就可以装X了。。。。
- python爬虫爬取好看的图片
- python爬虫----简单的图片爬取
- python爬虫爬取图片
- Python可自动登录爬取图片的网络爬虫
- [python爬虫]如何爬取特定网页的图片
- 【Python爬虫】爬取个人博客的图片
- Python爬虫——爬取网站的图片
- Python爬虫学习笔记(1):简单的图片爬取
- [python爬虫]--爬取mzitu.com的图片并保存
- Python爬虫---爬取静态网页的图片
- python 3.3 爬虫之爬取图片
- python爬虫爬取美女图片
- 《python爬虫实战》:爬取图片
- python爬虫-爬取美女图片
- python网络爬虫爬取图片代码
- Python爬虫爬取百度图片
- python爬虫实战 爬取天极图片
- python 小爬虫爬取美女图片
- Truck History (Prim算法)
- [PlantSimulation]Transporter运输工具的详细用法(二)
- HDU1015(暴力枚举)
- html5+css3
- 在有UEFI引导系统下的win10中安装双系统ubuntu的注意点
- python爬虫爬取好看的图片
- 敏捷开发模式在项目实际开发过程中一些问题
- HDU
- Uva839 Not so Mobile(天平)
- ElasticSearch Nginx形式的访问控制
- python3:module
- 快速打开文件命令行的三种方法
- 算法学习笔记--3.Binary Search & Algorithmic Complexity
- zlib window上编译