python爬虫——爬取妹子网美女图片 - Go语言中文社区

python爬虫——爬取妹子网美女图片


爬取目标

本文将爬取妹子图网站所有妹子图片。

爬取分析

我们发现当我们点击下一页时,url 会在原先的 url 基础上加上 /page/2,最后面的数字代表当前页面。

通过检查网页源代码我们发现表示页面的数字在 <a class="page-numbers"> 中

python爬虫——爬取妹子网美女图片

下面我们来获取所有页面数据

domain = 'http://www.mzitu.com'
start_html = requests.get(domain, headers=Hostreferer)
soup = BeautifulSoup(start_html.text, "lxml")
page = soup.find_all('a', class_='page-numbers')
max_page = page[-2].text # 最后一页显示的是下一页,不需要

为了方便查看图片,我们将图片标题作为文件夹,每个文件夹存放相应的图片。

python爬虫——爬取妹子网美女图片

下面我们来获取图片的标题并存入 E 盘的 mzitu 文件夹中。

url = 'http://www.mzitu.com/page/'
for n in range(1, int(max_page) + 1):
url += str(n)
start_html = requests.get(url, headers=Hostreferer)
soup = BeautifulSoup(start_html.text, "lxml")
targets = soup.find('div', class_='postlist').find_all('a', target='_blank')
for each in targets:
title = each.get_text() # 提取文本
if title:
print(f'正在爬取:{title}')
if os.path.exists(path + title):
flag = 1
else:
os.makedirs(path + title)
flag = 0
os.chdir(path + title) # 切换到你指定的文件夹中


随便点进去一张图我们会发现里面包含几十张图片,页面数据在 <div class="pagenavi"中,>下面我们来获取这几十张图片

python爬虫——爬取妹子网美女图片

url = 'http://www.mzitu.com/page/'
for n in range(1, int(max_page) + 1):
url += str(n)
start_html = requests.get(url, headers=Hostreferer)
soup = BeautifulSoup(start_html.text, "lxml")
targets = soup.find('div', class_='postlist').find_all('a', target='_blank')
for each in targets:
title = each.get_text() # 提取文本
if title:
print(f'正在爬取:{title}')
if os.path.exists(path + title):
flag = 1
else:
os.makedirs(path + title)
flag = 0
os.chdir(path + title)
href = each['href']
html = requests.get(href, headers=Hostreferer)
mess = BeautifulSoup(html.text, "lxml")
pic_max = mess.find(class_='pagenavi').find_all('span')
pic_max = pic_max[-2].text # 最大页数
for num in range(1, int(pic_max) + 1):
pic = href + '/' + str(num)
html = requests.get(pic, headers=Hostreferer)
mess = BeautifulSoup(html.text, "lxml")
pic_url = mess.find('img', alt=title)
html = requests.get(pic_url['src'], headers=Picreferer)
file_name = pic_url['src'].split(r'/')[-1]
with open(file_name, 'wb') as f:
f.write(html.content)

python爬虫——爬取妹子网美女图片



完整代码

该代码并没有采用多进程实现爬取,也没有封装,只提供学习使用,建议爬取5页图片就行,不然会卡顿和耗时较多。

import requests
from bs4 import BeautifulSoup
import os

Hostreferer = {
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
'Referer': 'http://www.mzitu.com'
}
Picreferer = {
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
'Referer': 'http://i.meizitu.net'
}
# 此请求头破解盗链


# 保存地址
path = 'E:/mzitu/'

# 找寻最大页数
# domain = 'http://www.mzitu.com'
# start_html = requests.get(domain, headers=Hostreferer)
# soup = BeautifulSoup(start_html.text, "lxml")
# page = soup.find_all('a', class_='page-numbers')
# max_page = page[-2].text
max_page = int(input('请输入爬取页面数量:')) # 建议大家别全部爬取
url = 'http://www.mzitu.com/page/'
for n in range(1, int(max_page) + 1):
url += str(n)
start_html = requests.get(url, headers=Hostreferer)
soup = BeautifulSoup(start_html.text, "lxml")
targets = soup.find('div', class_='postlist').find_all('a', target='_blank')
for each in targets:
title = each.get_text() # 提取文本
if title:
print(f'正在爬取:{title}')
if os.path.exists(path + title):
flag = 1
else:
os.makedirs(path + title)
flag = 0
os.chdir(path + title)
href = each['href']
html = requests.get(href, headers=Hostreferer)
mess = BeautifulSoup(html.text, "lxml")
pic_max = mess.find(class_='pagenavi').find_all('span')
pic_max = pic_max[-2].text # 最大页数
if flag == 1 and len(os.listdir(path + title)) >= int(pic_max):
print('已经保存完毕,跳过')
continue
for num in range(1, int(pic_max) + 1):
pic = href + '/' + str(num)
html = requests.get(pic, headers=Hostreferer)
mess = BeautifulSoup(html.text, "lxml")
pic_url = mess.find('img', alt=title)
html = requests.get(pic_url['src'], headers=Picreferer)
file_name = pic_url['src'].split(r'/')[-1]
with open(file_name, 'wb') as f:
f.write(html.content)
print(f'第{n}页爬取完成')

最后建议大家别把图片全部爬完,因为你的硬盘存不下,全部爬完得要不少时间。而且我们是为了学习,没必要爬这么多,做人留一线,日后好相见。图片虽好,可不要贪杯哦。!

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_34249678/article/details/86751996
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 17:40:53
  • 阅读 ( 2281 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢