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. 1st 结对编程:简易四则运算

    结对编程:简易四则运算 功能:进行简易的四则运算,并根据给出的结果判断正误. 实现:使用java的图形化界面实现. 导入包库 package six; import javax.swing.*; im ...

  2. 理解promise 02

    1:promise是什么? 就是(链式)包装的回调函数. 2:语法 new Promise( function(resolve, reject) {...} /* executor */ ); exe ...

  3. Spring Cloud 之 Eureka

    Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...

  4. Maven 项目依赖 pom 文件模板

    下面是网上down的 pom 文件模板: <!-- 属性 --> <properties> <spring.version>4.2.4.RELEASE</sp ...

  5. Spring 学习 3- AOP

    什么是AOP aop就是纵向的编程,业务1和业务2都需要一个共同的操作,与其往每个业务中都添加同样的代码,不如写一遍代码,让两个业务共同使用这段代码. spring中面向切面编程用的是代理模式,它的实 ...

  6. Vue2.0 - 全局操作 Vue.set

    引:http://www.cnblogs.com/zccblog/p/7192420.html Vue.set 的作用就是在构造器外部操作构造器内部的数据.属性或者方法.比如在vue构造器内部定义了一 ...

  7. RGB555转RGB565

    做tft彩屏显示图片的时候,显示16位位图,显示屏的显示模式为RGB565.使用img2lcd转换后的16位bmp,显示出来后,颜色有偏差:转换为565格式的bin文件,显示完全正常,可以确定转换为b ...

  8. bzoj1050[HAOI2006]旅行comf(枚举+贪心+并查集)

    Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求一条路径,使得路径上最大 ...

  9. 秒杀多线程第十四篇 读者写者问题继 读写锁SRWLock (续)

    java 包实现了读写锁的操作: package com.multithread.readwritelock; import java.util.concurrent.CountDownLatch; ...

  10. 二维RMQ模板

    int main(){ ; i <= n; i++) ; j <= m; j++) { scanf("%d", &val[i][j]); dp[i][j][][ ...