import os
from time import sleep

import faker
import requests
from lxml import etree

fake = faker.Faker()

base_url = "http://angelimg.spbeen.com"

def get_next_link(url):
content = downloadHtml(url)
html = etree.HTML(content)
next_url = html.xpath("//a[@class='ch next']/@href")
if next_url:
return base_url + next_url[0]
else:
return False

def downloadHtml(ur):
user_agent = fake.user_agent()
headers = {'User-Agent': user_agent,"Referer":"http://angelimg.spbeen.com/"}
response = requests.get(url, headers=headers,timeout=20)
if response.status_code != 200:
return None
else:
return response.text

def getImgUrl(content):
html = etree.HTML(content)
img_url = html.xpath('//*[@id="content"]/a/img/@src')
title = html.xpath(".//div['@class=article']/h2/text()")

return img_url[0],title[0]

def saveImg(title,img_url):
if img_url is not None and title is not None:

title = title.split('【')[0]
file_path = 'isssss/{}/'.format(title)
if not os.path.exists(file_path):
os.makedirs(file_path)
file_name = img_url.split('/')[-1]

with open(file_path+file_name+".jpg",'wb') as f:
user_agent = fake.user_agent()
headers = {'User-Agent': user_agent,"Referer":"http://angelimg.spbeen.com/"}
content = requests.get(img_url, headers=headers,timeout=20)
#request_view(content)
f.write(content.content)
print("save img "+ img_url)
f.close()

def request_view(response):
import webbrowser
request_url = response.url
base_url = '<head><base href="%s">' %(request_url)
base_url = base_url.encode()
content = response.content.replace(b"<head>",base_url)
tem_html = open('tmp.html','wb')
tem_html.write(content)
tem_html.close()
webbrowser.open_new_tab('tmp.html')

def optimizeContent(res):
res = res.replace('b\'', '')
res = res.replace('\\n', '')
res = res.replace('\'', '')
res = res.replace('style', 'nouse')
res = res.replace('\.', '')
return res

def crawl_img(url):
content = downloadHtml(url)
if content is not None:
res = getImgUrl(content)
title = res[1]
img_url = res[0]
title = optimizeContent(title)
title = title.replace('.', '')
print(title)
saveImg(title,img_url)
return True
else:
return None
if __name__ == "__main__":
try:

root_url = "http://angelimg.spbeen.com/ang/{}"

for i in range(37,10000):
url = root_url.format(i)
try:
while url:
res = crawl_img(url)
if res is None:
print(url + ' 无数据')
next = i + 1
url = root_url.format(next)
break
else:
url = get_next_link(url)
print("爬取页面:" + url)
i = i + 1
except Exception as e:
print(str(e))
except Exception as e:
print(str(e))

结果

python 爬虫 循环分页的更多相关文章

  1. python爬虫循环导入MySql数据库

    1.开发环境 操作系统:win10    Python 版本:Python 3.5.2   MySQL:5.5.53 2.用到的模块 没有的话使用pip进行安装:pip install xxx     ...

  2. Python爬虫:如何爬取分页数据?

    上一篇文章<Python爬虫:爬取人人都是产品经理的数据>中说了爬取单页数据的方法,这篇文章详细解释如何爬取多页数据. 爬取对象: 有融网理财项目列表页[履约中]状态下的前10页数据,地址 ...

  3. Python爬虫入门教程 2-100 妹子图网站爬取

    妹子图网站爬取---前言 从今天开始就要撸起袖子,直接写Python爬虫了,学习语言最好的办法就是有目的的进行,所以,接下来我将用10+篇的博客,写爬图片这一件事情.希望可以做好. 为了写好爬虫,我们 ...

  4. Python爬虫(四)——豆瓣数据模型训练与检测

    前文参考: Python爬虫(一)——豆瓣下图书信息 Python爬虫(二)——豆瓣图书决策树构建 Python爬虫(三)——对豆瓣图书各模块评论数与评分图形化分析 数据的构建 在这张表中我们可以发现 ...

  5. Python 爬虫实战(二):使用 requests-html

    Python 爬虫实战(一):使用 requests 和 BeautifulSoup,我们使用了 requests 做网络请求,拿到网页数据再用 BeautifulSoup 解析,就在前不久,requ ...

  6. python 爬虫(转,我使用的python3)

      原文地址:http://blog.csdn.net/pi9nc/article/details/9734437 [Python]网络爬虫(一):抓取网页的含义和URL基本构成 分类: 爬虫 Pyt ...

  7. 史诗级干货-python爬虫之增加CSDN访问量

    史诗级干货-python爬虫之增加CSDN访问量 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...

  8. 教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神

    本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http://www.xiaohuar.com/,让你体验爬取校花的成就感. Scr ...

  9. Python爬虫入门

    Python爬虫简介(来源于维基百科): 网络爬虫始于一张被称作种子的统一资源地址(URLs)列表.当网络爬虫访问这些统一资源定位器时,它们会甄别出页面上所有的超链接,并将它们写入一张"待访列表",即 ...

随机推荐

  1. jmeter连数据库

    前提:jmeter不能直接连数据库,需要导入一个jar包 步骤: 1.右键线程组--添加--配置元件--JDBC Connection Configuration 2.jdbc的基本配置:可以修改jd ...

  2. oracle之二表的几种类型

    Oracle中表的几种类型 1.表的功能:存储.管理数据的基本单元(二维表:有行和列组成)2.表的类型: 1)堆表:heap table :数据存储时,行是无序的,对它的访问采用全表扫描. 2)分区表 ...

  3. Vue 登录/登出以及JWT认证

    1. 后端代码概览 server/router/index.js 请求 router.get('/getUserInfo', function (req, res, next) { // 登录请求 r ...

  4. java 多线程-4

    十四.sleep方法和wait方法的区别 [面试题] 相同点: 一旦执行方法,都可以使得当前线程进入阻塞状态. 不同点: 两个方法的声明位置不同:Thread类声明sleep():Object类中声明 ...

  5. Combine 框架,从0到1 —— 4.在 Combine 中执行异步代码

    本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 -- 4.在 Combine 中执行异步代码. 内容概览 前言 用 Future 取代回调闭包 用输出类型( ...

  6. Object类:又回到最初的起点

    Object类大概是每个JAVA程序员认识的第一个类,因为它是所有其他类的祖先类.在JAVA单根继承的体系下,这个类中的每个方法都显得尤为重要,因为每个类都能够调用或者重写这些方法.当你JAVA学到一 ...

  7. Oracle学习(十四)分表分区

    一.前言 大数据量的查询,不仅查询速度非常慢,而且还会导致数据库经常宕机,在尝试添加索引及查询方式修改后,还有没有更有效的解决方案呢? 分库.分表.分区这些概念咱就应该了解一下. 二.分表 假如一个大 ...

  8. Java源码赏析(三)初识 String 类

    由于String类比较复杂,现在采用多篇幅来讲述 这一期主要从String使用的关键字,实现的接口,属性以及覆盖的方法入手.省略了大部分的字符串操作,比如split().trim().replace( ...

  9. IDEA搭建多模块maven项目

    目录 新建maven项目作为多模块的parent项目 新建模块 在parent项目中配置 pom api 模块搭建restful风格api Mybatis Generator的使用(mybatis 逆 ...

  10. Quartz.NET集成UI版

    Quartz.NET Quartz.NET是NET的开源作业调度系统. Quartz.NET是一个功能齐全的开源作业调度系统,可用于从最小的应用程序到大型企业系统. Quartz.NET目前支持NET ...