python有大佬帮帮忙改一下代码吗?
本帖最后由 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) 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 =
return await asyncio.gather(*tasks)
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())
CONCURRENCY 是并发数,10000为任务总量,需要python版本3.7以上,依赖协程。
具体请求内容看楼主需要自行修改 本帖最后由 15820394839 于 2022-5-25 12:29 编辑
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以上,可以使用异步请求。看楼主需要
页:
[1]