python 爬虫 循环分页
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 爬虫 循环分页的更多相关文章
- python爬虫循环导入MySql数据库
1.开发环境 操作系统:win10 Python 版本:Python 3.5.2 MySQL:5.5.53 2.用到的模块 没有的话使用pip进行安装:pip install xxx ...
- Python爬虫:如何爬取分页数据?
上一篇文章<Python爬虫:爬取人人都是产品经理的数据>中说了爬取单页数据的方法,这篇文章详细解释如何爬取多页数据. 爬取对象: 有融网理财项目列表页[履约中]状态下的前10页数据,地址 ...
- Python爬虫入门教程 2-100 妹子图网站爬取
妹子图网站爬取---前言 从今天开始就要撸起袖子,直接写Python爬虫了,学习语言最好的办法就是有目的的进行,所以,接下来我将用10+篇的博客,写爬图片这一件事情.希望可以做好. 为了写好爬虫,我们 ...
- Python爬虫(四)——豆瓣数据模型训练与检测
前文参考: Python爬虫(一)——豆瓣下图书信息 Python爬虫(二)——豆瓣图书决策树构建 Python爬虫(三)——对豆瓣图书各模块评论数与评分图形化分析 数据的构建 在这张表中我们可以发现 ...
- Python 爬虫实战(二):使用 requests-html
Python 爬虫实战(一):使用 requests 和 BeautifulSoup,我们使用了 requests 做网络请求,拿到网页数据再用 BeautifulSoup 解析,就在前不久,requ ...
- python 爬虫(转,我使用的python3)
原文地址:http://blog.csdn.net/pi9nc/article/details/9734437 [Python]网络爬虫(一):抓取网页的含义和URL基本构成 分类: 爬虫 Pyt ...
- 史诗级干货-python爬虫之增加CSDN访问量
史诗级干货-python爬虫之增加CSDN访问量 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...
- 教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http://www.xiaohuar.com/,让你体验爬取校花的成就感. Scr ...
- Python爬虫入门
Python爬虫简介(来源于维基百科): 网络爬虫始于一张被称作种子的统一资源地址(URLs)列表.当网络爬虫访问这些统一资源定位器时,它们会甄别出页面上所有的超链接,并将它们写入一张"待访列表",即 ...
随机推荐
- tcp建立连接为什么需要三次握手和四次挥手
前言 众所周知tcp传输层协议在建立连接的时候需要三次才能建立起一个真正的可靠连接,可是为什么是三次呢,不可以是两次,四次等等呢,可以自己思考一番,带着疑问可以看下文. 三次握手 在<计算机网络 ...
- 本机ping不通虚拟机,但虚拟机可以ping通本机
在各自网络都连接的情况下,本机ping不通虚拟机,但虚拟机可以ping通本机时解决方案: 1.linux虚拟机中连接方式选择NAT模式 2.本地启动VMnet8,然后选择VMnet8的属性,手动输入和 ...
- JS中条件判断语句
用pycharm敲代码时,在.js文件中敲,敲完之后复制代码到浏览器的console中去回车执行 1.if( ){} 2.switch(a){} 3.for循环 循环列表 循环字典 循环字符串 另外 ...
- sql注入 --显错注入
前提知识 数据库:就是将大量数据把保存起来,通过计算机加工而成的可以高效访问数据库的数据集合数据库结构:库:就是一堆表组成的数据集合表:类似 Excel,由行和列组成的二维表字段:表中的列称为字段记录 ...
- SpringCloud实战 | 第四篇:SpringCloud整合Gateway实现API网关
一. 前言 微服务实战系列是基于开源微服务项目 有来商城youlai-mall 版本升级为背景来开展的,本篇则是讲述API网关使用Gateway替代Zuul,有兴趣的朋友可以进去给个star,非常感谢 ...
- ES6重度学习 demo实例
let 与 const // 并非真正的常量 // const 的本质: const 定义的变量并非常量,并非不可变, // 它定义了一个常量引用一个值.使用 const 定义的对象或者数组,其实是可 ...
- JVM垃圾收集机制
JVM垃圾回收机制是java程序员必须要了解的知识,对于程序调优具有很大的帮助(同时也是大厂面试必问题). 要了解垃圾回收机制,主要从三个方面: (1)垃圾回收面向的对象是谁? (2)垃圾回收算法有哪 ...
- 关于微信小程序官网的使用
我们在看微信支付相关的东西的时候,会发现有些想找的地址不好找,,没看到入口,接下来我就是整理了一下 链接: https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa ...
- python中浅拷贝和深拷贝的区别
浅拷贝 可变类型浅拷贝copy函数就是浅拷贝,只对可变类型的第一层对象进行拷贝,对拷贝的对象开辟新的内存空间进行存储,不会拷贝对象内部的子对象可变类型:a = [1, 2, 3] b = [11, 2 ...
- Centos-显示开机信息-dmesg
dmesg 显示开机信息,开机时内核将开机信息存储在系统缓冲区(ring buffer)中,存储在 /var/log/dmesg文件中