首页 > 编程语言 >python高性能异步爬虫

python高性能异步爬虫

时间:2022-12-11 23:32:58浏览次数:54  
标签:异步 accumulo get python edu 爬虫 content time apache

目的:

在爬虫中使用异步实现高性能的数据爬取操作。

异步爬虫的方式:

1、多线程,多进程(不建议):

好处:可以为相关阻塞的操作单独开启线程,阻塞操作就可以异步执行。

弊端:无法无限制的开启多线程或者多进程。

2、线程池、进程池(适当的使用):

好处:可以降低系统对进程或者线程创建和销毁的一个频率,从而很好的降低系统的开销。

弊端:池中线程或进程的数量是有上限。

原则:线程池处理的是阻塞且耗时的操作。


不加线程池的操作:

# coding:utf-8
import requests
import time

headers = {
"User-Agent": "Mozilla/5.0(Windows NT 6.1;WOW64) AppleWebKit/537.36(KABUL, like Gecko) "
"Chrome/86.0.4240.198Safari/537.36 "
}
urls = [
'https://mirrors.tuna.tsinghua.edu.cn/apache/accumulo/1.10.2/accumulo-1.10.2-bin.tar.gz',
'https://mirrors.tuna.tsinghua.edu.cn/apache/accumulo/2.1.0/accumulo-2.1.0-bin.tar.gz',
'https://mirrors.tuna.tsinghua.edu.cn/apache/activemq/5.16.5/apache-activemq-5.16.5-bin.zip'
]

def get_content(url):
print("正在获取:", url)
# get方法是一个阻塞的方法
resp = requests.get(url, headers=headers)
if resp.status_code == 200:
return resp.content


def parse_content(content):
print("响应数据的长度为:", len(content))

start_time = time.time()
for url in urls:
content = get_content(url)
parse_content(content)
end_time = time.time()
print('%d second'% (end_time-start_time))


python高性能异步爬虫_线程池


增加线程池的操作:

# coding:utf-8
# coding:utf-8
import requests
import time
from multiprocessing.dummy import Pool

headers = {
"User-Agent": "Mozilla/5.0(Windows NT 6.1;WOW64) AppleWebKit/537.36(KABUL, like Gecko) "
"Chrome/86.0.4240.198Safari/537.36 "
}
urls = [
'https://mirrors.tuna.tsinghua.edu.cn/apache/accumulo/1.10.2/accumulo-1.10.2-bin.tar.gz',
'https://mirrors.tuna.tsinghua.edu.cn/apache/accumulo/2.1.0/accumulo-2.1.0-bin.tar.gz',
'https://mirrors.tuna.tsinghua.edu.cn/apache/activemq/5.16.5/apache-activemq-5.16.5-bin.zip'
]

def get_content(url):
print("正在获取:", url)
# get方法是一个阻塞的方法
resp = requests.get(url, headers=headers)
if resp.status_code == 200:
return resp.content


def parse_content(content):
print("响应数据的长度为:", len(content))

start_time = time.time()

# 实例化一个线程池对象
pool = Pool(3)
# 将列表中的每一个列表元素传递给get_content中进行处理。
pool.map(get_content, urls)

end_time = time.time()
print('%d second'% (end_time-start_time))

python高性能异步爬虫_线程池_02

线程池的应用:

from multiprocessing.dummy import Pool

urls = [
'https://mirrors.tuna.tsinghua.edu.cn/apache/accumulo/1.10.2/accumulo-1.10.2-bin.tar.gz',
'https://mirrors.tuna.tsinghua.edu.cn/apache/accumulo/2.1.0/accumulo-2.1.0-bin.tar.gz',
'https://mirrors.tuna.tsinghua.edu.cn/apache/activemq/5.16.5/apache-activemq-5.16.5-bin.zip'
]

start_time = time.time()

# 实例化一个线程池对象
pool = Pool(3)
# 将列表中的每一个列表元素传递给get_content中进行处理。
pool.map(get_content, urls)

end_time = time.time()
print('%d second'% (end_time-start_time))


标签:异步,accumulo,get,python,edu,爬虫,content,time,apache
From: https://blog.51cto.com/u_14012524/5928932

相关文章

  • python服务端与android客户端基于TCP协议的简单通信
    点击连接服务器后接收传来的数据,改变Textview的文字内容在服务器端建立一个py文件server.pyimportsockethost='10.0.1.15''''上边这里填服务器的内网地址我也不知......
  • 使用Python解析Windows系统日志
    目标要求:对Windows系统日志进行处理,并生成统计文件1.如何找到Windows系统日志?通常情况下,我们都是在Windows系统自带的事件查看器查看系统日志(使用win+x可以快速......
  • python 字符串的格式化
    什么是格式化定义:一个固定的字符串中有部分元素是根据变量的值而改变的字符串今天是xx,星期xx,大家好date='2022.1211'day='--' 根据类型定义的格式化'myname......
  • 使用网络爬虫自动抓取图书信息
    网络爬虫是一种从互联网上进行开放数据采集的重要手段。本案例通过使用Python的相关模块,开发一个简单的爬虫。实现从某图书网站自动下载感兴趣的图书信息的功能。主要实现的......
  • 异步批处理教程
    书接上回大数据量、高并发业务怎么优化?(一)文章中介绍了异步批处理的三种方式,本文继续深入针对前两种进行讲解,并给出代码示例:一普通版本,采用阻塞队列 ArrayBlockingQue......
  • 网络爬虫之re模块
    1.re.match尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。importreline="thishdr-biz123modelserver456"pattern=r"123"mat......
  • 进入python的世界_day49_Django的基本配置、ORM、前后端数据库的相联
    ​ 接口就是一个网址一、静态文件​ 不需要经常改变的文件,主要针对HTML文件所用到的资源,在django中,要提前手动创建一个文件夹,static,然后里面自己再分门别类一下#比如......
  • python字符串中返回bool类型的函数集合
    #isspaceistitleisupperislower#isspace判断字符串是否是一个由空格组成的字符串booltype=string.isspace()->无参数可传,返回一个布尔类型#由空格组成的字符串......
  • Python 替换字典里的字符串
    替换字典值,路径中的符号>>>a={"cover":"cover_images\\test.png"}>>>>>>a=eval(str(a).replace("\\\\","/"))>>>>>>print(a){'cover':'cover_images/test.pn......
  • python 正则表达式匹配
    在python中使用正则表达式以下几个步骤,1.用importre导入正则表达式模块2.用re.compile()函数创建一个Regex对象(记得使用原始字符串)3.向Regex对象的Search()方法传入想查找......