Python加速请求

Python加速请求,python,python-requests,Python,Python Requests,我正在创建一个小python脚本,通过用户名查找网站、论坛等页面,您可以轻松理解它。我将使用request,但是在一个搜索和另一个搜索之间,它确实非常慢。你有什么建议吗?提前谢谢 import requests from printy import printy, inputy username = inputy("[?] Username: ", "b") fivehundredpx = requests.get('https://www.500px.com/'+ username)

我正在创建一个小python脚本,通过用户名查找网站、论坛等页面,您可以轻松理解它。我将使用request,但是在一个搜索和另一个搜索之间,它确实非常慢。你有什么建议吗?提前谢谢

import requests
from printy import printy, inputy

username = inputy("[?] Username: ", "b")



fivehundredpx = requests.get('https://www.500px.com/'+ username)

if fivehundredpx.status_code == 200:
    printy('500px Found: https://www.500px.com/'+ username, "n")
elif fivehundredpx.status_code == 404:
    printy('500px Not Found', "r")



sevencups = requests.get('https://www.7cups.com/@'+ username)

if sevencups.status_code == 200:
    printy('7Cups Found: https://www.7cups.com/@'+ username, "n")
elif sevencups.status_code == 404:
    printy('7Cups Not Found', "r")

您不能真正控制服务器处理请求的速度,也不能控制网络延迟,但是您可以使用多线程和所需的并发级别来提高有效的RPS

下面是一个代码片段,它展示了如何实现多个请求的处理以提高总体处理速度。当涉及到IO时,希望这不是一个真正的问题,您可以真正获得一个数量级的性能改进

from concurrent.futures import ThreadPoolExecutor

def parse_skeleton(parse_fn, start=1, end=1_000_000, concurrency=10):

    with ThreadPoolExecutor(max_workers=concurrency, thread_name_prefix='parser') as executor:

        for id in range(start, end):
            executor.submit(parse_fn, id)

您不能真正控制服务器处理请求的速度,也不能控制网络延迟,但是您可以使用多线程和所需的并发级别来提高有效的RPS

下面是一个代码片段,它展示了如何实现多个请求的处理以提高总体处理速度。当涉及到IO时,希望这不是一个真正的问题,您可以真正获得一个数量级的性能改进

from concurrent.futures import ThreadPoolExecutor

def parse_skeleton(parse_fn, start=1, end=1_000_000, concurrency=10):

    with ThreadPoolExecutor(max_workers=concurrency, thread_name_prefix='parser') as executor:

        for id in range(start, end):
            executor.submit(parse_fn, id)

这可能更适合see:这可能更适合see: