利用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"))


运行后就可以获取得到str类型的html源码


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
原创粉丝点击