python爬取妹子图全站全部图片-可自行添加-线程-进程爬取,图片去重
from bs4 import BeautifulSoup
import sys,os,requests,pymongo,time
from lxml import etree
def get_fenlei(url,headers):
"""
获取妹子图的分类链接,并创建相对应的分类文件夹
:param url:
:param headers:
:return:
"""
response = requests.get(url,headers=headers).text
soup = BeautifulSoup(response,'lxml')
fenlei_url = soup.select('#menu-nav > li > a')
list1 = []
print(sys.path)
for i in fenlei_url:
fen_lei_lianjie = i['href']
if ''.join(fen_lei_lianjie).split('/')[3] =='all':
continue
elif ''.join(fen_lei_lianjie).split('/')[3] != '':
fenlei_name = i.get_text()
print(fenlei_name,fen_lei_lianjie,'首页')
get_fenlei_xia(fen_lei_lianjie,fenlei_name)
list1.append(fen_lei_lianjie)
def get_fenlei_xia(fen_lei_lianjie,fenlei_name):
""" :param fen_lei_lianjie:
:param fenlei_name:
:return:
"""
print('{}{}'.format(fen_lei_lianjie,'<><><><><><>'))
response = requests.get(fen_lei_lianjie,headers=headers).text
html = etree.HTML(response)
fenye_page = html.xpath('/html/body/div[2]/div[1]/div[2]/nav/div/a[4]')
page_list = []
if fenye_page != []:
for i in fenye_page:
page_shu = i.xpath('./text()')[0]
page_url = i.xpath('./@href')[0]
for ia in range(1,int(page_shu)+1):
fenlei_url = '{}/{}/{}/{}/'.format('https://www.mzitu.com/',''.join(page_url).split('/')[3],'page',ia)
page_list.append(fenlei_url)
else:
print('{}'.format('没有数据11111'))
fenye_page2 = html.xpath('//*[@id="comments"]/div/a[3]')
if fenye_page2 != []:
for aa in fenye_page2:
shuliang_shu = aa.xpath('./text()')[0]
shuliang_url = aa.xpath('./@href')[0]
for page in range(1,int(shuliang_shu)+3):
shen_url = '{}{}/{}/{}/{}'.format('https://www.mzitu.com/',''.join(shuliang_url).split('/')[3],'comment-page-',page,'#comments')
page_list.append(shen_url)
else:
print('{}'.format('没有数据222222'))
for shu in page_list:
get_all_url(shu,fenlei_name) def get_all_url(shu,fenlei_name):
"""
爬取分页下的图片链接
:param shu:
:param fenlei_name:
:return:
"""
print(shu,'<><><><><><><><>')
response = requests.get(shu,headers=headers).text
soup = BeautifulSoup(response,'lxml')
img_src = soup.select('#pins > li > a')
page_list = []
for i in img_src:
img_src = i['href']
page_list.append(img_src)
get_img_gref(img_src,fenlei_name)
def get_img_gref(img_src,fenlei_name):
"""
下载图片
:param img_src:
:param fenlei_name:
:return:
"""
print(img_src,'<><><><><><><><><><><<')
try:
response = requests.get(img_src,headers=headers)
print(response.status_code,'页面状态码')
html = etree.HTML(response.text)
img_href = html.xpath('//div[2]/div[1]/div[3]/p/a/img/@src')[0]
lei_name = html.xpath('/html/body/div[2]/div[1]/div[2]/span[1]/a/text()')[0]
if fenlei_name == lei_name:
wenjian = 'D:\\web_xiangmu\\biquge_tushu\\妹子图\\' + fenlei_name
if not os.path.exists(wenjian):
os.makedirs(wenjian)
print('{}{}{}'.format('D:\\web_xiangmu\\biquge_tushu\\妹子图\\<<<', fenlei_name, '>>>分类文件夹创建成功'))
else:
print(print('{}{}{}'.format('D:\\web_xiangmu\\biquge_tushu\\妹子图\\<<<', fenlei_name, '>>>分类文件夹已存在')))
"""
图片去重
"""
img_name = ''.join(img_href).split('/')[-1]
res = requests.get(img_href, headers=headers)
root_dir = 'D:\\web_xiangmu\\biquge_tushu\\妹子图\\' + fenlei_name
panduan_root_dir = 'D:\\web_xiangmu\\biquge_tushu\妹子图\\{}\\{}'.format(fenlei_name,img_name)
if os.path.exists(panduan_root_dir):
print('{}{}'.format(img_name,'已存在'))
else:
with open(root_dir + "\\" + img_name, 'wb') as f:
f.write(res.content)
f.close()
print(fenlei_name + '---' + img_name + '文件保存成功')
"""
图片去重
"""
else:
return None
except:
print('《《《连接失败》》》') def main():
return get_fenlei(url,headers) if __name__ == '__main__':
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
'Referer': 'https://www.mzitu.com/hot/', }
url = "https://www.mzitu.com/"
main()
python爬取妹子图全站全部图片-可自行添加-线程-进程爬取,图片去重的更多相关文章
- Python协程爬取妹子图(内有福利,你懂得~)
项目说明: 1.项目介绍 本项目使用Python提供的协程+scrapy中的选择器的使用(相当好用)实现爬取妹子图的(福利图)图片,这个学会了,某榴什么的.pow(2, 10)是吧! 2.用到的知 ...
- Python3爬虫系列:理论+实验+爬取妹子图实战
Github: https://github.com/wangy8961/python3-concurrency-pics-02 ,欢迎star 爬虫系列: (1) 理论 Python3爬虫系列01 ...
- Python-爬取妹子图(单线程和多线程版本)
一.参考文章 Python爬虫之——爬取妹子图片 上述文章中的代码讲述的非常清楚,我的基本能思路也是这样,本篇文章中的代码仅仅做了一些异常处理和一些日志显示优化工作,写此文章主要是当做笔记,方便以后查 ...
- Python网络爬虫 | Scrapy爬取妹子图网站全站照片
根据现有的知识,写了一个下载妹子图(meizitu.com)Scrapy脚本,把全站两万多张照片下载到了本地. 网站的分析 网页的网址分析 打开网站,发现网页的网址都是以 http://www.mei ...
- Python 2.7和3.6爬取妹子图网站单页测试图片
1.url= http://www.mzitu.com/74100/x,2为1到23的值 2.用到模块 os 创建文件目录; re模块正则匹配目录名 图片下载地址; time模块 限制下载时间;req ...
- Python 2.7_爬取妹子图网站单页测试图片_20170114
1.url= http://www.mzitu.com/74100/x,2为1到23的值 2.用到模块 os 创建文件目录; re模块正则匹配目录名 图片下载地址; time模块 限制下载时间;req ...
- 使用requests+BeaBeautiful Soup爬取妹子图图片
1. Requests:让 HTTP 服务人类 Requests 继承了urllib2的所有特性.Requests支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动确定 ...
- Python 爬取 妹子图(技术是无罪的)
... #!/usr/bin/env python import urllib.request from bs4 import BeautifulSoup def crawl(url): header ...
- Python 爬取妹子图(技术是无罪的)
... import requests from bs4 import BeautifulSoup import os import sys class mzitu(): def html(self, ...
随机推荐
- 树及其衍生算法(Trees and tree algorithms)
1,二叉树(Binary tree) 二叉树:每一个节点最多两个子节点,如下图所示: 相关概念:节点Node,路径path,根节点root,边edge,子节点 children,父节点parent,兄 ...
- TreeView 三种状态 没多大变化 只是增加了很多函数以方便调用
using System.Drawing; using System.Windows.Forms; using System.ComponentModel; namespace SimpleCusto ...
- 处理输入为非对角阵的Clustering by fast search and find of density peak代码
Clustering by fast search and find of density peak. Alex Rodriguez, Alessandro Laio 是发表在Science上的一篇很 ...
- an extra named object property
Grunt supports the ability to split each task configuration into several separate configurations all ...
- centos7.6安装docker
先运行 yum update 然后卸载旧版本 yum remove docker \ docker-client \ docker-client-latest \ docker-common \ do ...
- Java数组(1):数组与多维数组
我们对数组的基本看法是,你可以创建它们,通过使用整型索引值访问它们的元素,并且他们的尺寸不能改变. 但是有时候我们需要评估,到底是使用数组还是更加灵活的工具.数组是一个简单的线性序列,这使得元素访问非 ...
- 这些喜闻乐见的Java面试知识点,你都掌握了吗?
最近分享了一些有关学习方法和经验的文章,得到了很多读者的反馈,恰巧大家在昨天推文中的投票里一直选择了"Java基础的复习方法"这一项,那么今天我们就谈谈这方面的内容吧. 其实对于J ...
- ipmitool管理工具
一.ipmitool简介 IPMI(Intelligent Platform Management Interface)智能平台管理接口 1.IPMI的核心是一个专用芯片/控制器(叫做服务器处理器或基 ...
- python之pip使用技巧
pip 镜像临时使用:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider永久:直接在user目录中创建一个pip目录:C: ...
- PHP学习(6)——代码重用与函数编写的一些注意事项
一个新的项目是这样创建的:它将已有的可重新利用的组件进行组合,并将新的开发难度降低到最小. 代码重用的好处:降低成本.提升可靠性和一致性. 1.使用require()和include()函数 使用一条 ...