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. xstream 解析xml报文

    一.xml一种格式的数据转换为对象 pom.xml引入 <!--javaBean和XML的双向转换--> <dependency> <groupId>com.tho ...

  2. MVC中ajax提交表单示例

    页面中: @using (Ajax.BeginForm("Login", "User", new AjaxOptions() { HttpMethod = &q ...

  3. [Code Festival 2017 qual A] B: flip

    题意 给出一个n行m列初始全白的矩阵,每次可以翻转一行/一列的全部格子的颜色.问任意次操作后能否使得恰好有k个黑色格子. n,m<=1000 分析 显然要么翻转一次要么不翻转. 最终黑色格子数只 ...

  4. 【bzoj5123】[Lydsy12月赛]线段树的匹配 树形dp+记忆化搜索

    题目描述 求一棵 $[1,n]$ 的线段树的最大匹配数目与方案数. $n\le 10^{18}$ 题解 树形dp+记忆化搜索 设 $f[l][r]$ 表示根节点为 $[l,r]$ 的线段树,匹配选择根 ...

  5. 数据结构开发(7):典型问题分析(Bugfix)

    0.目录 1.创建异常对象时的空指针问题 2.LinkList 中的数据元素删除 3.LinkList 中遍历操作与删除操作的混合使用 4.StaticLinkList 中数据元素删除时的效率问题 5 ...

  6. pbuilder编译构建工具分析

    1. 简介 pbuilder(personal Debian package builder)是ubuntu环境下维护debian包的专业工具,能够为每个deb包创建纯净的编译构建环境,自动解析和安装 ...

  7. LiveCD及Casper调研

    1.LiveCD原理 LiveCD本质上是ISO 9660/El Torito格式的CD-ROM. 下面对LiveCD涉及的各种技术做了简单的调研. 1.1. CD-ROM CD-ROM是一种光盘存储 ...

  8. 【CodeChef-SPCLN】Cleaning the Space

    https://odzkskevi.qnssl.com/7dfb262544887eff6fb35bfb444759d6?v=1502084197 做法是类似于最大割之类的东西,把每个碎片按照按钮拆点 ...

  9. CF335F Buy One, Get One Free 贪心

    题意: \(n\)个物品,每个物品有一个价格,买一个高价格的物品,可以选择免费得到一个价格严格低于这个物品的物品.求得到\(n\)个物品的最小代价. 题解: 神仙贪心-- 题目要求求出最小代价,相当于 ...

  10. Linux内核分析8

    周子轩 原创作品转载请注明出处  <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验目的: 使用gdb ...