import requests
import os
from bs4 import BeautifulSoup
import re # 初始地址
all_url = 'http://www.7160.com/xiaohua/'
#保存路径
path = 'H:/school_girl/' # 请求头
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.1.2107.204 Safari/537.36'
} #################################开始请求(多列表)#################################
html = requests.get(all_url,headers = header)
start_html = html.text.encode('iso-8859-1').decode('gbk') # 将gb2312转为UTF-8格式
#################################开始解析#################################
soup = BeautifulSoup(start_html,'lxml')
#查找最大页码
page = 255 # 同一路径
same_url = 'http://www.7160.com/xiaohua/' for n in range(1,int(page)+1):
ul = same_url + 'list_6_' + str(n) + '.html' ####################开始请求(单列表多元素)###############
html = requests.get(ul,headers = header)
start_html = html.text.encode('iso-8859-1').decode('gbk') ########################开始解析##########################
soup = BeautifulSoup(start_html,'lxml')
all_a = soup.find('div',class_='news_bom-left').find_all('a',target = '_blank')
for a in all_a:
title = a.get_text()
if title != '':
########################创建目录##########################
#win不能创建带?的目录
if (os.path.exists(path + title.strip().replace('?', ''))):
# print('目录已存在')
flag = 1
else:
os.makedirs(path + title.strip().replace('?', ''))
flag = 0
os.chdir(path + title.strip().replace('?', ''))
######################### END ########################### ###################开始请求(单元素)###############
print('准备爬取:' + title)
hrefs = a['href']
in_url = 'http://www.7160.com'
href = in_url + hrefs htmls = requests.get(href,headers = header)
html = htmls.text.encode('iso-8859-1').decode('gbk')
#######################开始解析###################### mess = BeautifulSoup(html,'lxml')
titles = mess.find('h1').text
pic_max = mess.find('div',class_ = 'itempage').find_all('a')[-2].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):
href = a['href']
hrefs = re.findall(r'.{14}',href)
href = "".join(hrefs)
if num == 1:
html = in_url + href + '.html'
else:
html = in_url + href + '_' + str(num) + ".html" ###################开始请求(单元素里的子元素)###############
htmls = requests.get(html,headers = header)
html = htmls.text.encode('iso-8859-1').decode('gbk')
#######################开始解析######################
mess = BeautifulSoup(html,'lxml')
pic_url = mess.find('img',alt = titles)
print(pic_url['src']) #########################开始下载#####################
html = requests.get(pic_url['src'],headers = header)
filename = pic_url['src'].split(r'/')[-1]
f = open(filename,'wb')
f.write(html.content)
f.close()
print('完成')
print('第',n,'页完成')

打印后的结果为:

准备爬取:
阳光下校花美女迷人桃花眼嘴
http://img.7160.com/uploads/allimg/180913/13-1P913102541.jpg
http://img.7160.com/uploads/allimg/180913/13-1P913102541-50.jpg
http://img.7160.com/uploads/allimg/180913/13-1P913102541-51.jpg
http://img.7160.com/uploads/allimg/180913/13-1P913102542.jpg
http://img.7160.com/uploads/allimg/180913/13-1P913102542-50.jpg
http://img.7160.com/uploads/allimg/180913/13-1P913102542-51.jpg
http://img.7160.com/uploads/allimg/180913/13-1P913102542-52.jpg
http://img.7160.com/uploads/allimg/180913/13-1P913102542-53.jpg
http://img.7160.com/uploads/allimg/180913/13-1P913102542-54.jpg
http://img.7160.com/uploads/allimg/180913/13-1P913102543.jpg
http://img.7160.com/uploads/allimg/180913/13-1P913102543-50.jpg
完成
准备爬取:
黑长直发美女学生日系风制服
http://img.7160.com/uploads/allimg/180912/13-1P912102159.jpg
http://img.7160.com/uploads/allimg/180912/13-1P912102159-50.jpg
http://img.7160.com/uploads/allimg/180912/13-1P912102159-51.jpg
http://img.7160.com/uploads/allimg/180912/13-1P912102159-52.jpg
http://img.7160.com/uploads/allimg/180912/13-1P912102200.jpg

爬虫实战--基于requests 和 Beautiful的7160美图网爬取图片的更多相关文章

  1. 爬虫实战--基于requests和beautifulsoup的妹子网图片爬取(福利哦!)

    #coding=utf-8 import requests from bs4 import BeautifulSoup import os all_url = 'http://www.mzitu.co ...

  2. [原创] Python3.6+request+beautiful 半次元Top100 爬虫实战,将小姐姐的cos美图获得

    1 技术栈 Python3.6 Python的版本 request 得到网页html.jpg等资源的lib beautifulsoup 解析html的利器 html5lib 指定beautifulso ...

  3. 基于requests模块的cookie,session和线程池爬取

    目录 基于requests模块的cookie,session和线程池爬取 基于requests模块的cookie操作 基于requests模块的代理操作 基于multiprocessing.dummy ...

  4. Python爬虫实战之Requests+正则表达式爬取猫眼电影Top100

    import requests from requests.exceptions import RequestException import re import json # from multip ...

  5. vue基于video.js实现视频播放暂停---切图网

    切图网是最早致力于PSD2HTML切图等web前端外包服务的,随着前端技术的更新迭代,现在也已经全面投入了vue的浪潮了,下面是vue中实现视频播放的方法. vue.js中引入video视频播放器 m ...

  6. 爬虫开发3.requests模块

    requests模块 - 基于如下5点展开requests模块的学习 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能 ...

  7. 【Python爬虫实战】微信爬虫

    所谓微信爬虫,即自动获取微信的相关文章信息的一种爬虫.微信对我们的限制是很多的,所以我们需要采取一些手段解决这些限制主要包括伪装浏览器.使用代理IP等方式http://weixin.sogou.com ...

  8. python动态网站爬虫实战(requests+xpath+demjson+redis)

    目录 前言 一.主要思路 1.观察网站 2.编写爬虫代码 二.爬虫实战 1.登陆获取cookie 2.请求资源列表页面,定位获得左侧目录每一章的跳转url(难点) 3.请求每个跳转url,定位右侧下载 ...

  9. python 网络爬虫全流程教学,从入门到实战(requests+bs4+存储文件)

    python 网络爬虫全流程教学,从入门到实战(requests+bs4+存储文件) requests是一个Python第三方库,用于向URL地址发起请求 bs4 全名 BeautifulSoup4, ...

随机推荐

  1. vue shorthands

    vue shorthands : & @ https://vuejs.org/v2/guide/syntax.html#Shorthands v-for https://vuejs.org/v ...

  2. delphi self 的使用

    delphi之self 在使用delphi的对象技术的时候,经常会看到一个词汇:self,它到底指的是什么呢? 我们还要从对象与类的关系谈起. 类是对将要创建的对象的性质的描述,是一种文档.这很重要: ...

  3. 深入解析ThreadLocal类

    先了解一下ThreadLocal类提供的几个方法: public T get() { } public void set(T value) { } public void remove() { } p ...

  4. 【.Net】win10 uwp unix timestamp 时间戳 转 DateTime

    有时候需要把网络的 unix timestamp 转为 C# 的 DateTime ,在 UWP 可以如何转换? 转换函数可以使用下面的代码 private static DateTime UnixT ...

  5. UVA11736_Debugging RAM

    题目绝对够水,我就不详细说明了. 直接上代码吧.只是提示一下要用 unsigned long long. (不知道我不用字典树为什么会超时,肿么搞的) #include <iostream> ...

  6. [二]SpringBoot 之 简单的接口

    (1)编写一个实体类Demo package me.shijunjie.entity; public class Demo { private long id; private String name ...

  7. Elasticsearch之基本操作

    elasticsearch是一个是开源的(Apache2协议),分布式的,RESTful的,构建在Apache Lucene之上的的搜索引擎. 它有很多特点例如Schema Free,Document ...

  8. Spring Boot系列教程八: Mybatis使用分页插件PageHelper

    一.前言 上篇博客中介绍了spring boot集成mybatis的方法,基于上篇文章这里主要介绍如何使用分页插件PageHelper.在MyBatis中提供了拦截器接口,我们可以使用PageHelp ...

  9. Linux系统启动详解(一)

    本篇主要以Centos为例,讲述整个Linux系统启动过程,包括了grub引导,initramfs流程,/sbin/init执行rc.sysinit及rc的大体流程. 另外,本篇有一个实例来说明,将整 ...

  10. hadoop(三)HDFS基础使用

    一.HDFS前言 1. 设计思想          分而治之:将大文件,大批量文件,分布式的存放于大量服务器上.以便于采取分而治之的方式对海量数据进行运算分析     2. 在大数据系统架构中的应用  ...