一 背景知识

 爬虫的本质就是一个socket客户端与服务端的通信过程,如果我们有多个url待爬取,只用一个线程且采用串行的方式执行,那只能等待爬取一个结束后才能继续下一个,效率会非常低。

需要强调的是:对于单线程下串行N个任务,并不完全等同于低效,如果这N个任务都是纯计算的任务,那么该线程对cpu的利用率仍然会很高,之所以单线程下串行多个爬虫任务低效,是因为爬虫任务是明显的IO密集型程序。

关于IO模型详见链接:http://www.cnblogs.com/linhaifeng/articles/7454717.html

  

二 同步、异步、回调机制

1、同步调用:即提交一个任务后就在原地等待任务结束,等到拿到任务的结果后再继续下一行代码,效率低下

import requests

def parse_page(res):
print('解析 %s' %(len(res))) def get_page(url):
print('下载 %s' %url)
response=requests.get(url)
if response.status_code == 200:
return response.text urls=['https://www.baidu.com/','http://www.sina.com.cn/','https://www.python.org']
for url in urls:
res=get_page(url) #调用一个任务,就在原地等待任务结束拿到结果后才继续往后执行
parse_page(res)

同步调用

2、一个简单的解决方案:多线程或多进程

#在服务器端使用多线程(或多进程)。多线程(或多进程)的目的是让每个连接都拥有独立的线程(或进程),这样任何一个连接的阻塞都不会影响其他的连接。
#IO密集型程序应该用多线程
import requests
from threading import Thread,current_thread def parse_page(res):
print('%s 解析 %s' %(current_thread().getName(),len(res))) def get_page(url,callback=parse_page):
print('%s 下载 %s' %(current_thread().getName(),url))
response=requests.get(url)
if response.status_code == 200:
callback(response.text) if __name__ == '__main__':
urls=['https://www.baidu.com/','http://www.sina.com.cn/','https://www.python.org']
for url in urls:
t=Thread(target=get_page,args=(url,))
t.start()

多进程或多线程

该方案的问题是:

#开启多进程或都线程的方式,我们是无法无限制地开启多进程或多线程的:在遇到要同时响应成百上千路的连接请求,则无论多线程还是多进程都会严重占据系统资源,降低系统对外界响应效率,而且线程与进程本身也更容易进入假死状态。

3、改进方案: 线程池或进程池+异步调用:提交一个任务后并不会等待任务结束,而是继续下一行代码

#很多程序员可能会考虑使用“线程池”或“连接池”。“线程池”旨在减少创建和销毁线程的频率,其维持一定合理数量的线程,并让空闲的线程重新承担新的执行任务。“连接池”维持连接的缓存池,尽量重用已有的连接、减少创建和关闭连接的频率。这两种技术都可以很好的降低系统开销,都被广泛应用很多大型系统,如websphere、tomcat和各种数据库等。
#IO密集型程序应该用多线程,所以此时我们使用线程池
import requests
from threading import current_thread
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor def parse_page(res):
res=res.result()
print('%s 解析 %s' %(current_thread().getName(),len(res))) def get_page(url):
print('%s 下载 %s' %(current_thread().getName(),url))
response=requests.get(url)
if response.status_code == 200:
return response.text if __name__ == '__main__':
urls=['https://www.baidu.com/','http://www.sina.com.cn/','https://www.python.org'] pool=ThreadPoolExecutor(50)
# pool=ProcessPoolExecutor(50)
for url in urls:
pool.submit(get_page,url).add_done_callback(parse_page) pool.shutdown(wait=True)

进程池或线程池:异步调用+回调机制

改进后方案其实也存在着问题:

#“线程池”和“连接池”技术也只是在一定程度上缓解了频繁调用IO接口带来的资源占用。而且,所谓“池”始终有其上限,当请求大大超过上限时,“池”构成的系统对外界的响应并不比没有池的时候效果好多少。所以使用“池”必须考虑其面临的响应规模,并根据响应规模调整“池”的大小。

 对应上例中的所面临的可能同时出现的上千甚至上万次的客户端请求,“线程池”或“连接池”或许可以缓解部分压力,但是不能解决所有问题。总之,多线程模型可以方便高效的解决小规模的服务请求,但面对大规模的服务请求,多线程模型也会遇到瓶颈,可以用非阻塞接口来尝试解决这个问题。

三 高性能

上述无论哪种解决方案其实没有解决一个性能相关的问题:IO阻塞,无论是多进程还是多线程,在遇到IO阻塞时都会被操作系统强行剥夺走CPU的执行权限,程序的执行效率因此就降低了下来。

解决这一问题的关键在于,我们自己从应用程序级别检测IO阻塞然后切换到我们自己程序的其他任务执行,这样把我们程序的IO降到最低,我们的程序处于就绪态就会增多,以此来迷惑操作系统,操作系统便以为我们的程序是IO比较少的程序,从而会尽可能多的分配CPU给我们,这样也就达到了提升程序执行效率的目的

1、在python3.3之后新增了asyncio模块,可以帮我们检测IO(只能是网络IO),实现应用程序级别的切换

import asyncio

@asyncio.coroutine
def task(task_id,senconds):
print('%s is start' %task_id)
yield from asyncio.sleep(senconds) #只能检测网络IO,检测到IO后切换到其他任务执行
print('%s is end' %task_id) tasks=[task(task_id="任务1",senconds=3),task("任务2",2),task(task_id="任务3",senconds=1)] loop=asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
loop.close()

基本使用

2、但asyncio模块只能发tcp级别的请求,不能发http协议,因此,在我们需要发送http请求的时候,需要我们自定义http报头

import asyncio
import requests
import uuid
user_agent='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0' def parse_page(host,res):
print('%s 解析结果 %s' %(host,len(res)))
with open('%s.html' %(uuid.uuid1()),'wb') as f:
f.write(res) @asyncio.coroutine
def get_page(host,port=80,url='/',callback=parse_page,ssl=False):
print('下载 http://%s:%s%s' %(host,port,url)) #步骤一(IO阻塞):发起tcp链接,是阻塞操作,因此需要yield from
if ssl:
port=443
recv,send=yield from asyncio.open_connection(host=host,port=443,ssl=ssl) # 步骤二:封装http协议的报头,因为asyncio模块只能封装并发送tcp包,因此这一步需要我们自己封装http协议的包
request_headers="""GET %s HTTP/1.0\r\nHost: %s\r\nUser-agent: %s\r\n\r\n""" %(url,host,user_agent)
# requset_headers="""POST %s HTTP/1.0\r\nHost: %s\r\n\r\nname=egon&password=123""" % (url, host,)
request_headers=request_headers.encode('utf-8') # 步骤三(IO阻塞):发送http请求包
send.write(request_headers)
yield from send.drain() # 步骤四(IO阻塞):接收响应头
while True:
line=yield from recv.readline()
if line == b'\r\n':
break
print('%s Response headers:%s' %(host,line)) # 步骤五(IO阻塞):接收响应体
text=yield from recv.read() # 步骤六:执行回调函数
callback(host,text) # 步骤七:关闭套接字
send.close() #没有recv.close()方法,因为是四次挥手断链接,双向链接的两端,一端发完数据后执行send.close()另外一端就被动地断开 if __name__ == '__main__':
tasks=[
get_page('www.baidu.com',url='/s?wd=美女',ssl=True),
get_page('www.cnblogs.com',url='/',ssl=True),
] loop=asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
loop.close()

asyncio+自定义http协议报头

3、自定义http报头多少有点麻烦,于是有了aiohttp模块,专门帮我们封装http报头,然后我们还需要用asyncio检测IO实现切换

import aiohttp
import asyncio @asyncio.coroutine
def get_page(url):
print('GET:%s' %url)
response=yield from aiohttp.request('GET',url) data=yield from response.read() print(url,data)
response.close()
return 1 tasks=[
get_page('https://www.python.org/doc'),
get_page('https://www.cnblogs.com/linhaifeng'),
get_page('https://www.openstack.org')
] loop=asyncio.get_event_loop()
results=loop.run_until_complete(asyncio.gather(*tasks))
loop.close() print('=====>',results) #[1, 1, 1]

asyncio+aiohttp

4、此外,还可以将requests.get函数传给asyncio,就能够被检测

import requests
import asyncio @asyncio.coroutine
def get_page(func,*args):
print('GET:%s' %args[0])
loog=asyncio.get_event_loop()
furture=loop.run_in_executor(None,func,*args)
response=yield from furture print(response.url,len(response.text))
return 1 tasks=[
get_page(requests.get,'https://www.python.org/doc'),
get_page(requests.get,'https://www.cnblogs.com/linhaifeng'),
get_page(requests.get,'https://www.openstack.org')
] loop=asyncio.get_event_loop()
results=loop.run_until_complete(asyncio.gather(*tasks))
loop.close() print('=====>',results) #[1, 1, 1]

asyncio+requests模块的方法

5、还有之前在协程时介绍的gevent模块

from gevent import monkey;monkey.patch_all()
import gevent
import requests def get_page(url):
print('GET:%s' %url)
response=requests.get(url)
print(url,len(response.text))
return 1 # g1=gevent.spawn(get_page,'https://www.python.org/doc')
# g2=gevent.spawn(get_page,'https://www.cnblogs.com/linhaifeng')
# g3=gevent.spawn(get_page,'https://www.openstack.org')
# gevent.joinall([g1,g2,g3,])
# print(g1.value,g2.value,g3.value) #拿到返回值 #协程池
from gevent.pool import Pool
pool=Pool(2)
g1=pool.spawn(get_page,'https://www.python.org/doc')
g2=pool.spawn(get_page,'https://www.cnblogs.com/linhaifeng')
g3=pool.spawn(get_page,'https://www.openstack.org')
gevent.joinall([g1,g2,g3,])
print(g1.value,g2.value,g3.value) #拿到返回值

gevent+requests

6、封装了gevent+requests模块的grequests模块

#pip3 install grequests

import grequests

request_list=[
grequests.get('https://wwww.xxxx.org/doc1'),
grequests.get('https://www.cnblogs.com/linhaifeng'),
grequests.get('https://www.openstack.org')
] ##### 执行并获取响应列表 #####
# response_list = grequests.map(request_list)
# print(response_list) ##### 执行并获取响应列表(处理异常) #####
def exception_handler(request, exception):
# print(request,exception)
print("%s Request failed" %request.url) response_list = grequests.map(request_list, exception_handler=exception_handler)
print(response_list)

grequests

7、twisted:是一个网络框架,其中一个功能是发送异步请求,检测IO并自动切换,scrapy框架就是基于twisted编写的 !!!!

'''
#问题一:error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
https://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
pip3 install C:\Users\Administrator\Downloads\Twisted-17.9.0-cp36-cp36m-win_amd64.whl
pip3 install twisted #问题二:ModuleNotFoundError: No module named 'win32api'
https://sourceforge.net/projects/pywin32/files/pywin32/ #问题三:openssl
pip3 install pyopenssl
''' #twisted基本用法
from twisted.web.client import getPage,defer
from twisted.internet import reactor def all_done(arg):
# print(arg)
reactor.stop() def callback(res):
print(res)
return 1 defer_list=[]
urls=[
'http://www.baidu.com',
'http://www.bing.com',
'https://www.python.org',
]
for url in urls:
obj=getPage(url.encode('utf=-8'),)
obj.addCallback(callback)
defer_list.append(obj) defer.DeferredList(defer_list).addBoth(all_done) reactor.run() #twisted的getPage的详细用法
from twisted.internet import reactor
from twisted.web.client import getPage
import urllib.parse def one_done(arg):
print(arg)
reactor.stop() post_data = urllib.parse.urlencode({'check_data': 'adf'})
post_data = bytes(post_data, encoding='utf8')
headers = {b'Content-Type': b'application/x-www-form-urlencoded'}
response = getPage(bytes('http://dig.chouti.com/login', encoding='utf8'),
method=bytes('POST', encoding='utf8'),
postdata=post_data,
cookies={},
headers=headers)
response.addBoth(one_done) reactor.run()

twisted的用法

8、tornado

from tornado.httpclient import AsyncHTTPClient
from tornado.httpclient import HTTPRequest
from tornado import ioloop def handle_response(response):
"""
处理返回值内容(需要维护计数器,来停止IO循环),调用 ioloop.IOLoop.current().stop()
:param response:
:return:
"""
if response.error:
print("Error:", response.error)
else:
print(response.body) def func():
url_list = [
'http://www.baidu.com',
'http://www.bing.com',
]
for url in url_list:
print(url)
http_client = AsyncHTTPClient()
http_client.fetch(HTTPRequest(url), handle_response) ioloop.IOLoop.current().add_callback(func)
ioloop.IOLoop.current().start() #发现上例在所有任务都完毕后也不能正常结束,为了解决该问题,让我们来加上计数器
from tornado.httpclient import AsyncHTTPClient
from tornado.httpclient import HTTPRequest
from tornado import ioloop count=0 def handle_response(response):
"""
处理返回值内容(需要维护计数器,来停止IO循环),调用 ioloop.IOLoop.current().stop()
:param response:
:return:
"""
if response.error:
print("Error:", response.error)
else:
print(len(response.body)) global count
count-=1 #完成一次回调,计数减1
if count == 0:
ioloop.IOLoop.current().stop() def func():
url_list = [
'http://www.baidu.com',
'http://www.bing.com',
] global count
for url in url_list:
print(url)
http_client = AsyncHTTPClient()
http_client.fetch(HTTPRequest(url), handle_response)
count+=1 #计数加1 ioloop.IOLoop.current().add_callback(func)
ioloop.IOLoop.current().start()

Tornado

爬虫高性能 asyncio库 twisted库 tornado库的更多相关文章

  1. 爬虫高性能asyncio+ahttpio

    async实现协程,异步编程 我们都知道,现在的服务器开发对于IO调度的优先级控制权已经不再依靠系统,都希望采用协程的方式实现高效的并发任务,如js.lua等在异步协程方面都做的很强大. python ...

  2. python网络爬虫学习笔记(二)BeautifulSoup库

    Beautiful Soup库也称为beautiful4库.bs4库,它可用于解析HTML/XML,并将所有文件.字符串转换为'utf-8'编码.HTML/XML文档是与“标签树一一对应的.具体地说, ...

  3. 爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,loads,dump,load方法介绍

    爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,load ...

  4. 基于canvas实现的高性能、跨平台的股票图表库--clchart

    什么是 ClChart? ClChart是一个基于canvas创建的简单.高性能和跨平台的股票数据可视化开源项目.支持PC.webApp以及React Native和Weex等平台.在React Na ...

  5. Python爬虫入门(二)之Requests库

    Python爬虫入门(二)之Requests库 我是照着小白教程做的,所以该篇是更小白教程hhhhhhhh 一.Requests库的简介 Requests 唯一的一个非转基因的 Python HTTP ...

  6. 【转】 iOS 开发之静态库.a和动态库详解 -- 不错

    原文网址:http://blog.csdn.net/lxl_815520/article/details/52154331 一, 简单介绍 1.什么是库 库是程序代码的集合,是共享程序代码的一种方式 ...

  7. iOS:Xcode7下创建 .a静态库 和 .framework静态库

    Xcode7 中创建静态库:.a 和 .framework 一.简单介绍 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的分类 根据源代码的公开情况,库可以分为2种类型 (1)开 ...

  8. iOS开发中静态库之".framework静态库"的制作及使用篇

    iOS开发中静态库之".framework静态库"的制作及使用篇 .framework静态库支持OC和swift .a静态库如何制作可参照上一篇: iOS开发中静态库之" ...

  9. [转]Linux下g++编译与使用静态库(.a)和动态库(.os) (+修正与解释)

    在windows环境下,我们通常在IDE如VS的工程中开发C++项目,对于生成和使用静态库(*.lib)与动态库(*.dll)可能都已经比较熟悉,但是,在linux环境下,则是另一套模式,对应的静态库 ...

随机推荐

  1. Python——序列化模块

    #json 模式 1.dumps.loads  方法 针对内存 dic = {'k1':'v1'} #转换成json import json str_d = json.dumps(dic) #序列化 ...

  2. 「洛谷3292」「BZOJ4568」「SCOI2016」幸运数字【倍增LCA+线性基+合并】

    [bzoj数据下载地址]不要谢我 先讲一下窝是怎么错的... \(MLE\)是因为数组开小了.. 看到异或和最大,那么就会想到用线性基. 如果不会线性基的可以参考一下我的学习笔记:「线性基」学习笔记a ...

  3. python 爬虫之beautifulsoup(bs4)使用 --待完善

    #!/usr/bin/env python # -*- coding:utf- -*- from bs4 import BeautifulSoup import requests url = 'htt ...

  4. 微服务之服务中心—Eureka

    Eureka 简介Eureka 是 Spring Cloud Netflix 的一个子模块,也是核心模块之一,用于云端服务发现,是一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故 ...

  5. Entity Framework入门教程(10)---离线场景保存和删除实体/实体图集

    离线场景保存和删除实体/实体图集 这一节的内容是在离线场景中保存实体和实体图集 在离线场景中,当我们保存一个离线的实体图集或一个单独的离线实体时,我们需要做两件事.首先,我们要把实体附加到新的上下文中 ...

  6. Ubuntu16下Hive 安装

    0.安装环境和版本 Ubuntu16,hadoop版本是2.7.2 ,选择Hive版本为  hive-2.1.17 1. Hive安装包下载 地址: https://mirrors.tuna.tsin ...

  7. Typora使用说明(记录总结)

    目录 区域元素 YAML FONT Matters 菜单 段落 标题 引注 序列 可选序列 代码块 数学块 表格 脚注 水平线 特征元素 链接 超链接 内链接 相关链 URLs 图片 斜体 加粗 删除 ...

  8. ElasticSearch评分分析 explian 解释和一些查询理解

    ElasticSearch评分分析 explian 解释和一些查询理解 按照es-ik分析器安装了ik分词器.创建索引:PUT /index_ik_test.索引包含2个字段:content和nick ...

  9. Select文字居右显示

    使用text-align: right;不起作用,应当使用direction: rtl;

  10. [物理学与PDEs]第5章习题9 伴随矩阵的特征值

    设 $3\times 3$ 阵 ${\bf A}$ 的特征值为 $\lm_1,\lm_2,\lm_3$, 证明 $\cof {\bf A}$ 的特征值为 $$\bex \lm_2\lm_3,\quad ...