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. Twisted框架学习

    Twisted是用Python实现的基于事件驱动的网络引擎框架,是python中一个强大的异步IO库.理解twisted的一个前提是弄清楚twisted中几个核心的概念: reactor, Proto ...

  2. instanceof 实现

    A instanceof B // 实现 instanceof function instance(a, b) { const proto = a.__proto__; // eslint-disab ...

  3. [go]socket编程

    socket特性 总是成对出现 是全双工的(同时支持收发)(两个channel绑在一起) 应用程序 - cs模式(客户端开发) - bs模式(web开发) net包api基础 都是客户端主动发数据(c ...

  4. 动态库(.so)隐藏函数名

    一.偶遇 error: undefined reference to  xxx 问题 尝试封装通用的接口到一个private.so,然后供客户端使用,private.so编译出来后由sample.cp ...

  5. 一百一十七:CMS系统之注册页面对接短信验证码

    from flask import Blueprint, requestfrom exts import alidayufrom utils import restfulfrom utils.capt ...

  6. Ubuntu配置zsh

    安装zsh sudo apt-get install zsh chsh -s /bin/zsh 配置 curl -L https://raw.github.com/robbyrussell/oh-my ...

  7. 开题报告中如何将一段文字插入到word表格中

    1,举例如下,打开空白word,设计一个20列的表格.任意一段文字. 2,选中这段文字,点击替换按钮.查找内容为“?”,替换为“^&,”(后面是逗号),并勾选“使用通配符”. 3,全部替换得到 ...

  8. React Native实战一

    效果图如下所示: 展示列表页面,点击跳转至详情页面:     /** * Sample React Native App * https://github.com/facebook/react-nat ...

  9. 基于Opencv的梯度及其方向

    我们都知道梯度很好求,只需要将[-1,1] 与图像分别在x 方向和y方向卷积,即可求得两个方向上的梯度.不过在求梯度方向时,还是有些麻烦,因为梯度方向会指向360°的任何一个方向,所以直接用atan( ...

  10. 酷Q插件_SDK———入门与使用

    1.这里使用的是易语言的SDK,所以请看不惯的人自行离开.{wymbf.} 1.2 这里不会直接给你工程,请用动手打代码,动脑思考. 1.3 不知道.不了解酷Q是干嘛的,请访问:https://cqp ...