利用urllib+beadutifulsoup编写自己的第一个小爬虫,获取美女图片
来源:互联网 发布:linux ubuntu安装 rpm 编辑:程序博客网 时间:2024/06/11 01:27
我要开始写博客啦!!!今天先从最基础的开始,爬取一个网站的妹子图
搭建环境
安装了Python3.6后,再安装BeautifulSoup便可以进行本次编程
先导入模块:
import urllib.requestfrom bs4 import BeautifulSoupimport os
准备工作完成,正式开始项目
本次爬虫是在妹子图网站: http://mzitu.com 爬取网站的所有的美女图片。
1.首先爬虫的入口便是:http://mzitu.com/all
开始编写代码,用来获取整个页面的html源码,用作之后的分析。
import urllib.requestfrom bs4 import BeautifulSoupimport os# 爬虫网站的入口url = "http://www.mzitu.com/all"response= urllib.request.urlopen(url)print(response.read().decode("utf-8"))
2. 由于前面获取的是一个str类型的字符串,我们通过将其转换为beautifulSoup对象
soup = BeautifulSoup(reponse)
3.我们在浏览器中打开妹子图网站,通过分析其源码,找到其中的图片所在的位置,通过分析,我们可以知道,图片在此网页的图片的特点是所有我们需要的图片都是在<div class="all"> 位置中所有的a标签就是我们所需要的
代码如下:
import urllib.requestfrom bs4 import BeautifulSoupimport os# 爬虫网站的入口url = "http://www.mzitu.com/all"response= urllib.request.urlopen(url)soup = BeautifulSoup(reponse)a_list = soup.find("div", class_="all").find_all("a")for a in a_list:text = a.get_text()href = a['href']print(text, href)4.获取了图片的基本信息之后,我们就可以通过urllib.request.urlretrieve来保存图片了,但是我们更像将这个网站的所有图片都保存下来,其实,该网站所有的url不同的图片合集的url变化的只是其最后的数字,而每一个图片合集的都有一个span标签标记页码,所以我们可以通过循环,获取到所有的页码,在将其和url想加,便是具体的地址。
import urllib.requestfrom bs4 import BeautifulSoupimport osurl = "http://www.mzitu.com/all"reponse = urllib.request.urlopen(url)soup = BeautifulSoup(reponse)a_list = soup.find('div', class_='all').find_all('a')for a in a_list: text = a.get_text() href = a['href'] html = urllib.request.urlopen(href) html_soup = BeautifulSoup(html) max_span = html_soup.find('div', class_='pagenavi').find_all('span')[-2].get_text() for page in range(1, int(max_span)+1): page_url = href + "/" + str(page) img_html = urllib.request.urlopen(page_url) img_soup = BeautifulSoup(img_html) img_url = img_soup.find('div', class_='main-image').find('img')['src']print("img_url")5.最后,由于我们就将这些这些网址urlopen,由于一直不间断的访问该网站,很容易被封ip,所以我们可以通过time.sleep或者通过代理来避开风险,在此我们引入time模块,来访问img的url, 保存在硬盘中,就此大功告成。import urllib.requestfrom bs4 import BeautifulSoupimport osimport timeurl = "http://www.mzitu.com/all"reponse = urllib.request.urlopen(url)soup = BeautifulSoup(reponse)a_list = soup.find('div', class_='all').find_all('a')for a in a_list: text = a.get_text() href = a['href'] os.makedirs(os.path.join("D:\mzitu", text)) os.chdir("D:\mzitu\\"+text) html = urllib.request.urlopen(href) html_soup = BeautifulSoup(html) max_span = html_soup.find('div', class_='pagenavi').find_all('span')[-2].get_text() for page in range(1, int(max_span)+1): page_url = href + "/" + str(page) img_html = urllib.request.urlopen(page_url) img_soup = BeautifulSoup(img_html) img_url = img_soup.find('div', class_='main-image').find('img')['src'] name = img_url[-9:-4] urllib.request.urlretrieve(img_url, filename="D:\mzitu\%s\%s.jpg"%(text,name))time.sleep(0.1)
运行该程序,就可以获取到萌萌的妹子的图片辣!!!
1 0
- 利用urllib+beadutifulsoup编写自己的第一个小爬虫,获取美女图片
- 第一个小爬虫-------下载jpg图片
- python 小爬虫爬取美女图片
- 第一个小爬虫
- 我的第一个小爬虫
- 基于Nodejs的第一个小爬虫
- python的第一个小爬虫requests
- 小程序编写的美女
- 我写的第一个图片爬虫
- 自己编写的第一个程序
- 第一个python程序,小爬虫--抓取网页图片
- 第一个小爬虫--爬取图片并保存
- 小虫出世----第一个爬虫程序(扒下慕课网站的图片)
- 【Python爬虫】编写第一个爬虫
- 新浪微博美女图片的爬虫
- 第一个爬虫小程序
- 第一个爬虫小程序
- 自己写的第一个小网站
- 动态规划(01背包、完全背包、多重部分和、LCS、LIS、划分数、多重集组合数)
- 类(2)
- QCache 缓存
- 性能优化二 高性能的索引策略
- 生成树的计数Matrix-Tree定理
- 利用urllib+beadutifulsoup编写自己的第一个小爬虫,获取美女图片
- layer-list实现阴影和选择器效果
- 正规式转确定有穷自动机(NFA)
- CCF消除类游戏JAVA答案
- uva694 The Collatz Sequence
- POJ3494
- 关于Pyhton虚拟环境
- 01-复杂度1 最大子列和问题
- Linux下stat + 文件名后, Access,Modify,Change的含义