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, ...
随机推荐
- centos7 下安装和配置 mongodb (重点)
1.下载安装包 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.4.tgz 2.解压 tar -zxvf m ...
- http2.0多路复用
http/1中的每个请求都会建立一个单独的连接,除了在每次建立连接过程中的三次握手之外,还存在TCP的慢启动导致的传输速度低.其实大部分的http请求传送的数据都很小,就导致每一次请求基本上都没有达到 ...
- Nginx优化之日志优化,URL访问控制,防盗链,及站点文件目录优化
Nginx日志相关优化与安全 日志切割脚本如下: #!/bin #日志切割脚本 Date=`date +%Y%m%d` Bdir="/usr/local/nginx" Nginxl ...
- 升级到Android Studio3.x遇到的问题及解决方案
升级到Android Studio3.x遇到的问题及解决方案 转 https://www.2cto.com/kf/201711/695736.html 升级到Android Studio3.0遇到的问 ...
- [Ubuntu]18安装百度网盘
1.下载客户端 下载地址: 选择linux版本,我选择的是deb格式,下载就可以了. 2.安装 进入下载目录,点击右键,选择在终端打开. 之后输入 以下代码愉快的安装就好了 注意:dpkg后面跟的文 ...
- idea忽略隐藏文件、文件夹的设置操作
左上角setting 如果要忽略文件夹,则直接填写文件夹名字即可,例如:要忽略target文件夹[建议:尽量不要把target忽略,因为可能编译出问题排查,还需要查看target文件夹中的编译结果] ...
- 微服务的脚手架Jhipster使用(一)
随着微服务的普及以及docker容器的广泛应用,有传统的soa服务衍生出微服务的概念,微服务强调的是服务的独立性,屏蔽底层物理平台的差异,此时你会发现微服务跟容器技术完美契合.在此基础上衍生出的云原生 ...
- 用python做线性规划
scipy.optimize.linprog(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bounds=None, method='simplex', ...
- Cloudera Manager 常见异常
1. Cloudera Management Service服务全部无法启动 观察到的现象: (1)cm service 组件无法启动,启动时服务请求超时终止:(2)主机信息也无法获取到,一直提示“无 ...
- 编译安装了的nginx 添加http_ssl_module模块
1.看下编译安装nginx的时候,都编译安装的哪些模块. [root@zabbix ~]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1. ...