python 爬取妹子图
作为一个python还没入门的小白,搞懂这段代码实在是很不容易,还要去学html的知识(#黑脸)
因此我加上了注释,比较好读懂点
#coding=utf-8
import time
import requests
from bs4 import BeautifulSoup
import os
import sys if(os.name == 'nt'):
print(u'你正在使用win平台')
else:
print(u'你正在使用linux平台') header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
#http请求头
all_url = 'http://www.mzitu.com'
start_html = requests.get(all_url,headers = header) #保存地址
path = 'D:/mzitu/' #找寻最大页数
soup = BeautifulSoup(start_html.text,"html.parser") #定义为beautifulsoup类parser
page = soup.find_all('a',class_='page-numbers') #page为一个列表,找到标签为a的,属性class为‘page-numbers'的标签
max_page = page[-2].text #观察最大页数在-2处,用.text取文本信息 same_url = 'http://www.mzitu.com/page/'
for n in range(1,int(max_page)+1):
ul = same_url+str(n)
start_html = requests.get(ul, headers=header)
soup = BeautifulSoup(start_html.text,"html.parser")#标签的寻找find是一对一对的然后用text取其中内容
all_a = soup.find('div',class_='postlist').find_all('a',target='_blank')#在find div的大类下找到所有标签a且a标签中target为blank的标签(因为有多个a类标签,需要把没用的排除掉)
for a in all_a: #all_a一共找到两个部分,第一个部分是空的,第二部分才能提取得到title
title = a.text #提取文本内容就是在<>尖括号以外的信息
if(title != ''):
print("准备扒取:"+title) #win不能创建带?的目录 如果目录中含有?将其替换为''
if(os.path.exists(path+title.strip().replace('?',''))):#如果目录存在(exist)返回1,flag = 1
flag=1
else:
os.makedirs(path+title.strip().replace('?',''))
flag=0
os.chdir(path + title.strip().replace('?',''))#改变当前工作目录到指定路径
href = a['href']#图片地址
html = requests.get(href,headers = header)
mess = BeautifulSoup(html.text,"html.parser")
pic_max = mess.find_all('span')
pic_max = pic_max[10].text #最大页数
if(flag == 1 and len(os.listdir(path+title.strip().replace('?',''))) >= int(pic_max)):
print('已经保存完毕,跳过')
continue
for num in range(1,int(pic_max)+1):
time.sleep(1)#抓慢一点,上次爬的太快ip被封了
pic = href+'/'+str(num)#图片所在页的地址
html = requests.get(pic,headers = header)
mess = BeautifulSoup(html.text,"html.parser")
pic_url = mess.find('img',alt = title)#找到标签为img
html = requests.get(pic_url['src'],headers = header)#这里找到的才是真正图片的地址
file_name = pic_url['src'].split(r'/')[-1]#文件名为地址分割后的最后一个字符串
f = open(file_name,'wb')
f.write(html.content)#content是类函数,在这里就是地址html所表示的图片
f.close()
print('完成')
print('第',n,'页完成')
python 爬取妹子图的更多相关文章
- Python 爬虫入门(二)——爬取妹子图
Python 爬虫入门 听说你写代码没动力?本文就给你动力,爬取妹子图.如果这也没动力那就没救了. GitHub 地址: https://github.com/injetlee/Python/blob ...
- Python 爬虫入门之爬取妹子图
Python 爬虫入门之爬取妹子图 来源:李英杰 链接: https://segmentfault.com/a/1190000015798452 听说你写代码没动力?本文就给你动力,爬取妹子图.如果 ...
- 小白学 Python 爬虫(16):urllib 实战之爬取妹子图
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- scrapy 也能爬取妹子图?
目录 前言 Media Pipeline 启用Media Pipeline 使用 ImgPipeline 抓取妹子图 瞎比比前言 我们在抓取数据的过程中,除了要抓取文本数据之外,当然也会有抓取图片的需 ...
- 使用request+Beautiful爬取妹子图
一.request安装 pip install requests request使用示例 import requests response = requests.get('https://www.mz ...
- requests+正则表达式 爬取 妹子图
做了一个爬取妹子图某张索引页面的爬虫,主要用request和正则表达式. 感谢 崔庆才大神的 爬虫教学视频 和 gitbook: B站:https://www.bilibili.com/video/a ...
- 爬取妹子图(requests + BeautifulSoup)
刚刚入门爬虫,今天先对于单个图集进行爬取,过几天再进行翻页爬取. 使用requests库和BeautifulSoup库 目标网站:妹子图 今天是对于单个图集的爬取,就选择一个进行爬取,我选择的链接为: ...
- 利用 PhpQuery 随机爬取妹子图
前言 运行下面的代码会随机得到妹子图的一张图片,代码中的phpQuery可以在这里下载:phpQuery-0.9.5.386.zip <?php require 'phpQuery.php'; ...
- Python爬虫个人记录(三)爬取妹子图
这此教程可能会比较简洁,具体细节可参考我的第一篇教程: Python爬虫个人记录(一)豆瓣250 Python爬虫个人记录(二)fishc爬虫 一.目的分析 获取煎蛋妹子图并下载 http://jan ...
随机推荐
- 根据wsdl文件,soupUI生成webservice客户端代码
根据wsdl文件,soupUI生成webservice客户端代码 功能介绍: 对于面向WebServie接口开发时,当我们已经获取到WSDL文件后,可以使用soapUI工具生成对应的客户端和服务端代码 ...
- Android 拖动条 和 Handle
- Python3基础 生成器推导式 简单示例
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 详解C中的volatile关键字【转】
本文转载自:http://www.cnblogs.com/yc_sunniwell/archive/2010/06/24/1764231.html volatile提醒编译器它后面所定义的变量随时都有 ...
- 32位MD5加密补齐丢失的0
/// <summary> /// 获取32位MD5加密字符串(已补完0) /// </summary> /// <param name="strWord&qu ...
- BZOJ3300: [USACO2011 Feb]Best Parenthesis 模拟
Description Recently, the cows have been competing with strings of balanced parentheses and compari ...
- Linux的内存分页管理【转】
内存是计算机的主存储器.内存为进程开辟出进程空间,让进程在其中保存数据.我将从内存的物理特性出发,深入到内存管理的细节,特别是了解虚拟内存和内存分页的概念. 内存 简单地说,内存就是一个数据货架.内存 ...
- shell :
示例一.(用作注释,占位)#!/bin/bash : this is single line comment : 'this is a multiline comment, second line e ...
- Springboot 如何加密,以及利用Swagger2构建Restful API
先看一下使用Swagger2构建Restful API效果图 超级简单的,只需要在pom 中引用如下jar包 <dependency> <groupId>io.springfo ...
- Cassandra学习笔记
CASSANDRA在工作中用过,但是用的项目少,能用却了解的不全面.今天来稍加学习下: http://zqhxuyuan.github.io/2015/10/15/Cassandra-Daily/ ...