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)列表.当网络爬虫访问这些统一资源定位器时,它们会甄别出页面上所有的超链接,并将它们写入一张"待访列表",即 ...
随机推荐
- Java成神之路:第一帖---- Vue的组件属性components用法
Vue的组件属性:components 使用场景 一般在项目的使用过程中,某个需要多次使用的模块,会将整个模块抽取出来,写一个组件,供给其他页面进行调用或者是在一个页面中,多次使用到一个重复的代码样式 ...
- SpringCloud OpenFeign Post请求的坑
在微服务开发中SpringCloud全家桶集成了OpenFeign用于服务调用,SpringCloud的OpenFeign使用SpringMVCContract来解析OpenFeign的接口定义. 但 ...
- xml的复习
xml的复习 1.概念:可扩展标记语言 2.功能: *存储数据 1.配置文件 2.在网络中传播 3.xml与html区别: xml语法严格,HTML语法松散 xml存储数据,HTML展示 ...
- 智慧出行--maas
未来智慧出行新生态——MaaS系统的解读与畅想 -城市交通:观察与思考 - 未来智慧出行新生态——MaaS系统的解读与畅想 在货运领域,有一种承运方式叫“多式联运”,它是由承运人与货主签订一份货运合同 ...
- .NET 5 中 Target Framework 详解
作者:.NET Team 翻译:精致码农-王亮 原文:http://dwz.win/Q4v 我们希望极大地简化开发人员必须在项目文件和 NuGet 包中使用的TFM (Target Framework ...
- vue项目工程中npm run dev 到底做了什么
1. npm install 安装了webpack框架中package.json中所需要的依赖 2.安装完成之后,需要启动整个项目运行,npm run 其实执行了package.json中的scrip ...
- 关于button和submit的form提交以及 页面跳转问题
最近在做官网的注册登录form提交时遇到了这个问题,1.0时因为使用普通的模板并没有出现页面跳转失败问题 由于2.0时更换了注册模板,此时按钮样式是以下样式 而在css样式的模板里使用的是button ...
- Oracle 11gR2
OracleOraDb11g_home1TNSListener #其它客服端连接需要开启服务,如不开启,本机连接可以直接使用sqlplus OracleServiceORCL #实例SID服务 sq ...
- sping cloud入门
可以参考原文 https://www.cnblogs.com/sam-uncle/archive/2018/04/25/8943471.html 注册服务 https://www.cnblogs.co ...
- linux_基础调优
1. 配置授时服务,使用阿里云的授时服务 echo -e "# update time\n*/5 * * * * /usr/sbin/ntpdate time1.aliyun.com &am ...