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. [LeetCode]67. 二进制求和(字符串)(数学)

    题目 给你两个二进制字符串,返回它们的和(用二进制表示). 输入为 非空 字符串且只包含数字 1 和 0. 题解 两个字符串从低位开始加,前面位不够补0.维护进位,最后加上最后一个进位,最后反转结果字 ...

  2. 4.Kafka使用

  3. 国内外比较不错的php框架汇总

    国外 1.laravel 2.symfony 3.yii 4.laminas(zendframework) 国内 1.thinkphp 2.ebcms 3.暂无

  4. python爬取新浪财经

    我们来获取这里的title和url然后再获取这里面url的编辑作者 可以看到右边的几个就对应的左边不同的div .m-p1-mb2-list.m-list-container ul li a impo ...

  5. windows服务器中创建账号及管理相关的net命令

    本文测试环境:windows server 2012 R2 Datacenter 实例要求: 1.创建账号,加入到远程桌面组,能实现远程桌面登录 2.指定Full name .及Description ...

  6. 使用jpa的注意事项

    @Transactional 事务注解 nativeQuery = true表示使用写的sql,不是HQL@Modifying 注解完成修改操作使用jap时,在实体类的类名上面加上 @Entity@T ...

  7. 刷题[MRCTF2020]套娃

    解题思路 查看源码,发现注释中存在代码 //1st $query = $_SERVER['QUERY_STRING']; if( substr_count($query, '_') !== 0 || ...

  8. Kafka监控必备——Kafka-Eagle 2.0.2正式发布

    对于经常使用Kafka的同学,拥有一个炫酷又实用的监控系统是非常有必要的.可以实时的监控数据流的情况,了解实时数据流的变化. Kafka Eagle Kafka Eagle是一个监控系统,监控Kafk ...

  9. Python练习题 019:求分数序列之和

    [Python练习题 019] 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和. --------------------------------- ...

  10. 解决SpringBoot项目创建缓慢问题

    SpringBoot项目构建缓慢 快速创建springboot项目 在创建一个springboot项目的时候,往往速度会很慢,原因是下载springboot文件的默认地址是springboot官网(国 ...