吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 618|回复: 2
收起左侧

[求助] python有大佬帮帮忙改一下代码吗?

[复制链接]
jin920619 发表于 2022-5-24 23:02
50吾爱币
本帖最后由 jin920619 于 2022-5-24 23:51 编辑

学了一下POST,打算并发和循环POST,运行了一下发现太慢了,基本上2秒后接收返回数据后再循环,如何才能实现可以设置并发速度

import requests
while True:
    headers1 = {

}

    json_data1 = {

}




    headers2 = {

    }

    json_data2 = {

    }




    headers3 = {

    }

    json_data3 = {

    }


    headers4 = {

    }

    json_data4 = {

    }


    headers5 = {

    }

    json_data5 = {

    }


    headers6 = {

    }

    json_data6 = {

    }


    headers7 = {

    }

    json_data7 = {
      
    }


    headers8 = {

    }

    json_data8 = {
        
    }


    headers9 = {

    }

    json_data9 = {
        
    }

    response1 = requests.post('1', headers=headers1, json=json_data1)
    response2 = requests.post('1', headers=headers2, json=json_data2)
    response3 = requests.post('1', headers=headers3, json=json_data3)
    response4 = requests.post('1', headers=headers4, json=json_data4)
    response5 = requests.post('1', headers=headers5, json=json_data5)
    response6 = requests.post('1', headers=headers6, json=json_data6)
    response7 = requests.post('1', headers=headers7, json=json_data7)
    response8 = requests.post('1', headers=headers8, json=json_data8)
    response9 = requests.post('1', headers=headers9, json=json_data9)


    print(response1.text)
    print(response2.text)
    print(response3.text)
    print(response4.text)
    print(response5.text)
    print(response6.text)
    print(response7.text)
    print(response8.text)
    print(response9.text)

最佳答案

查看完整内容

[mw_shl_code=python,true]import asyncio import aiohttp CONCURRENCY = 5 URL = 'https://www.baidu.com' JSON_DATA = { } HEADERS = { } semaphore = asyncio.Semaphore(CONCURRENCY) async def fetch(): async with semaphore: async with aiohttp.ClientSession() as session: async with session.post(URL, headers=HEADERS, json=JSON_DATA) as response: ...

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

RichardYangZT 发表于 2022-5-24 23:02
[Python] 纯文本查看 复制代码
import asyncio
import aiohttp

CONCURRENCY = 5

URL = 'https://www.baidu.com'

JSON_DATA = {

}

HEADERS = {

}

semaphore = asyncio.Semaphore(CONCURRENCY)


async def fetch():
    async with semaphore:
        async with aiohttp.ClientSession() as session:
            async with session.post(URL, headers=HEADERS, json=JSON_DATA) as response:
                data =  await response.text()
                print(data)
                return data

async def main():
    tasks = [asyncio.ensure_future(fetch()) for _ in range(10000)]
    return await asyncio.gather(*tasks)
if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())

CONCURRENCY 是并发数,10000为任务总量,需要python版本3.7以上,依赖协程。
具体请求内容看楼主需要自行修改
choujie1689 发表于 2022-5-25 11:59
本帖最后由 15820394839 于 2022-5-25 12:29 编辑

[Python] 纯文本查看 复制代码
import requests
def main():
    urls = [
        "https://tenfei05.cfp.cn/creative/vcg/800/new/VCG41560336195.jpg",
        "https://tenfei03.cfp.cn/creative/vcg/800/new/VCG41688057449.jpg",
    ]
    json_data = [
        "nihao",
        "nihao1",
    ]
    headers = [
        "baidu",
        "baidu"
    ]
    
    for url,jsondata,header in zip(urls,json_data,headers):
        response = requests.post(url,jsondata,header)
        print(response.text)
main()
header头部和jsondata如果相同可以只用一个,不行的话,把url,jsondata,header都放在一个list里面,for循环遍历发送请求,如果python3.8以上,可以使用异步请求。看楼主需要
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 11:41

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表