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爬取妹子图全站全部图片-可自行添加-线程-进程爬取,图片去重的更多相关文章

  1. Python协程爬取妹子图(内有福利,你懂得~)

    项目说明: 1.项目介绍   本项目使用Python提供的协程+scrapy中的选择器的使用(相当好用)实现爬取妹子图的(福利图)图片,这个学会了,某榴什么的.pow(2, 10)是吧! 2.用到的知 ...

  2. Python3爬虫系列:理论+实验+爬取妹子图实战

    Github: https://github.com/wangy8961/python3-concurrency-pics-02 ,欢迎star 爬虫系列: (1) 理论 Python3爬虫系列01 ...

  3. Python-爬取妹子图(单线程和多线程版本)

    一.参考文章 Python爬虫之——爬取妹子图片 上述文章中的代码讲述的非常清楚,我的基本能思路也是这样,本篇文章中的代码仅仅做了一些异常处理和一些日志显示优化工作,写此文章主要是当做笔记,方便以后查 ...

  4. Python网络爬虫 | Scrapy爬取妹子图网站全站照片

    根据现有的知识,写了一个下载妹子图(meizitu.com)Scrapy脚本,把全站两万多张照片下载到了本地. 网站的分析 网页的网址分析 打开网站,发现网页的网址都是以 http://www.mei ...

  5. Python 2.7和3.6爬取妹子图网站单页测试图片

    1.url= http://www.mzitu.com/74100/x,2为1到23的值 2.用到模块 os 创建文件目录; re模块正则匹配目录名 图片下载地址; time模块 限制下载时间;req ...

  6. Python 2.7_爬取妹子图网站单页测试图片_20170114

    1.url= http://www.mzitu.com/74100/x,2为1到23的值 2.用到模块 os 创建文件目录; re模块正则匹配目录名 图片下载地址; time模块 限制下载时间;req ...

  7. 使用requests+BeaBeautiful Soup爬取妹子图图片

    1. Requests:让 HTTP 服务人类 Requests 继承了urllib2的所有特性.Requests支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动确定 ...

  8. Python 爬取 妹子图(技术是无罪的)

    ... #!/usr/bin/env python import urllib.request from bs4 import BeautifulSoup def crawl(url): header ...

  9. Python 爬取妹子图(技术是无罪的)

    ... import requests from bs4 import BeautifulSoup import os import sys class mzitu(): def html(self, ...

随机推荐

  1. ora-01578

    SQL> exec DBMS_STATS.GATHER_DATABASE_STATS; BEGIN DBMS_STATS.GATHER_DATABASE_STATS; END; * ERROR ...

  2. pip下载提速

    方法一使用国内镜像: 清华:https://pypi.tuna.tsinghua.edu.cn/simple/ 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中 ...

  3. Java并发包同步工具之Exchanger

    前言 承接上文Java并发包同步工具之Phaser,讲述了同步工具Phaser之后,搬家博客到博客园了,接着未完成的Java并发包源码探索,接下来是Java并发包提供的最后一个同步工具Exchange ...

  4. LC 241. Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  5. 表单中使用<button>的注意点

    本文主要记录了我调查问题的思路想法,想看结论的同学直接拖到最后吧 上周在做项目的时候,有一个需求是在页面中加一个按钮,点一下查询数据库将内容填充在表格中.这不是很简单嘛,页面加个按钮,发送ajax请求 ...

  6. 记一次ceph集群的严重故障 (转)

    问题:集群状态,坏了一个盘,pg状态好像有点问题[root@ceph-1 ~]# ceph -s    cluster 72f44b06-b8d3-44cc-bb8b-2048f5b4acfe     ...

  7. Heartbeat实现web服务器高可用

    一.Heartbeat概述: Heartbeat的工作原理:heartbeat最核心的包括两个部分,心跳监测部分和资源接管部分,心跳监测可以通过网络链路和串口进行,而且支持冗余链路,它们之间相互发送报 ...

  8. iscsi序列二、iscsi多路径配置方式

    一.ISCSI多路径应用 如果存储服务器到交换机只有一条线路的时候,那么一条线路出线故障,整个就没法使用了,所以多线路可以解决这个问题,避免单点故障 如上图,如果SAN服务器与客户端交换机只有一条线路 ...

  9. Linux Shell 自动备份脚本

    写一个使用shell脚本增量备份系统文件,顺便复习一下shell脚本相关的命令,这个脚本可以根据自己的需求来备份不同的文件或者文件夹,进行完整备份和增量备份. 参考的网址:http://blog.51 ...

  10. Python排序之多属性排序

    1.sort函数说明: sort函数是list类的一个方法,说明如下: sort(...)L.sort(cmp=None, key=None, reverse=False) -- stable sor ...