gpt4 book ai didi

python-3.x - 如何访问Gathering Future完成结果里面的数据

转载 作者:行者123 更新时间:2023-12-04 00:49:26 24 4
gpt4 key购买 nike

一直试图弄清楚我返回数据后如何访问我的数据。如果我打印它,这是 x 的示例:

<_GatheringFuture finished result=[[{'ip': '173.245.203.70'}], [{'ip': '196.52.2.82'}], [{'ip': '69.161.4.1'}], [{'ip':'73.180.140.205'}], [{'ip': '96.242.181.128'}], [{'ip': '108.208.74.183'}], ...]>

我尝试了 x[0]、x.result、x.results 等……但一直出现错误。有人知道如何访问 Future 结果中的数据吗?感谢您的时间!下面是完整的代码:
import asyncio
from bs4 import BeautifulSoup
from aiohttp import ClientSession

HEADERS = {'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
proxy = 'xxx.xxx.xxx.xxx:xxxxx'

async def fetch(url, session):
while True:
x = []
try:
async with session.get(url, headers=HEADERS, proxy="http://{}".format(proxy), timeout=9) as r:
print (r.status)
if r.status == 200:
data = await r.read()
soup = BeautifulSoup(data, 'html.parser')
for ip in soup.select('tr + tr td font'):
array.append({'ip' : ip.get_text()})
break
except:
pass
return x

async def bound_fetch(sem, url, session):
async with sem:
return await fetch(url, session)

async def run(num):
tasks = []
sem = asyncio.Semaphore(300)

async with ClientSession() as session:
for i in range(0,num):
task = asyncio.ensure_future(bound_fetch(sem, 'http://ip4.me', session))
tasks.append(task)

responses = asyncio.gather(*tasks)
await responses
return responses

if __name__ == '__main__':
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run(10))
x = loop.run_until_complete(future)
loop.close()
print (x)

最佳答案

这里responses你回来的是你从asyncio.gather得到的 future :

responses = asyncio.gather(*tasks)
await responses
return responses

你要的是这个 future 的结果。像这样改变你的代码:
responses = await asyncio.gather(*tasks)
return responses

关于python-3.x - 如何访问Gathering Future完成结果里面的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48829265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com