python并发请求_Python并发请求之requests_future模块使用

# -*- coding: utf-8 -*-

# @Time : 2019-12-09 10:00

# @Author : cxa

# @File : demo.py

# @Software: PyCharm

from requests_futures.sessions import FuturesSession

from concurrent.futures import as_completed

from lxml import html

import time

url = ["http://www.baidu.com", "http://www.163.com", "http://www.google.com", "http://www.cnblogs.com/c-x-a"]

def get_node(source, x=".//head/title//text()"):

root = html.fromstring(source)

node = root.xpath(x)

return node

def response_hook(resp, *args, **kwargs):

start = time.time()

resp.encoding = resp.apparent_encoding

resp.data = resp.text

resp.code = resp.status_code

resp.headers = resp.headers

resp.elapsed = time.time() - start

def get_req():

with FuturesSession(max_workers=4) as session:

futures = [session.get(i, hooks={"response": response_hook}) for i in url]

for future in as_completed(futures):

resp = future.result()

print("状态码", resp.code)

print("标题", get_node(resp.data)[0])

print("耗时", resp.elapsed)

print("="*30)

if __name__ == '__main__':

get_req()

修改之后

from requests_futures.sessions import FuturesSession

from concurrent.futures import as_completed

from lxml import html

import time

url = ["http://www.baidu.com", "http://www.163.com", "http://www.google.com", "http://www.cnblogs.com/c-x-a"]

class MySession(FuturesSession):

def request(self, method, url, hooks=None, *args, **kwargs):

start = time.time()

if hooks is None:

hooks = {}

def response_hook(resp, *args, **kwargs):

resp.encoding = resp.apparent_encoding

resp.data = resp.text

resp.code = resp.status_code

resp.headers = resp.headers

resp.elapsed = time.time() - start

try:

if isinstance(hooks['response'], (list, tuple)):

hooks['response'].insert(0, response_hook)

else:

hooks['response'] = [response_hook, hooks['response']]

except KeyError:

hooks['response'] = response_hook

return super(MySession, self).request(method, url, hooks=hooks, *args, **kwargs)

def get_node(source, x=".//head/title//text()"):

root = html.fromstring(source)

node = root.xpath(x)

return node

def get_req():

with MySession(max_workers=4) as session:

futures = [session.get(i) for i in url]

for future in as_completed(futures):

resp = future.result()

print("状态码", resp.code)

print("标题", get_node(resp.data)[0])

print("耗时", resp.elapsed)

print("=" * 30)

if __name__ == '__main__':

get_req()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值