gpt4 book ai didi

python - 使用aiohttp获取多个网站的状态

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

我有这个代码,用于获取网站列表的状态。

import aiohttp
import asyncio
import json
import sys
import time


async def get_statuses(websites):
statuses = {}
tasks = [get_website_status(website) for website in websites]
for status in await asyncio.gather(*tasks):
if not statuses.get(status):
statuses[status] = 0
statuses[status] += 1
print(json.dumps(statuses))


async def get_website_status(url):
response = await aiohttp.get(url)
status = response.status
response.close()
return status


if __name__ == '__main__':
with open(sys.argv[1], 'r') as f:
websites = f.read().splitlines()
t0 = time.time()
loop = asyncio.get_event_loop()
loop.run_until_complete(get_statuses(websites))
t1 = time.time()
print("getting website statuses took {0:.1f} seconds".format(t1-t0))

由于 get 已被贬值 await aiohttp.get(url) 我这样编辑了代码

import aiohttp
import asyncio
import json
import sys
import time

async def fetch(session, url):
async with session.get(url) as response:
return await response.text()

async def get_statuses(websites):
statuses = {}
tasks = [get_website_status(website) for website in websites]
for status in await asyncio.gather(*tasks):
if not statuses.get(status):
statuses[status] = 0
statuses[status] += 1
print(json.dumps(statuses))


async def get_website_status(url):
async with aiohttp.ClientSession() as session:
response = await fetch(session, url)
#response = await aiohttp.get(url)
status = response.status
response.close()
return status


if __name__ == '__main__':
with open(sys.argv[1], 'r') as f:
websites = f.read().splitlines()
t0 = time.time()
loop = asyncio.get_event_loop()
loop.run_until_complete(get_statuses(websites))
t1 = time.time()
print("getting website statuses took {0:.1f} seconds".format(t1-t0))

我从文档 https://aiohttp.readthedocs.io/en/stable/ 复制了 session 代码

但是,当我运行代码时,我收到此错误:

c:\asyncio>a.py list.txt
Traceback (most recent call last):
File "C:\asyncio\a.py", line 35, in <module>
loop.run_until_complete(get_statuses(websites))
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\asyncio\base_ev
ents.py", line 579, in run_until_complete
return future.result()
File "C:\asyncio\a.py", line 14, in get_statuses
for status in await asyncio.gather(*tasks):
File "C:\asyncio\a.py", line 25, in get_website_status
status = response.status
AttributeError: 'str' object has no attribute 'status'

c:\asyncio>

这是一个示例list.txt

https://facebook.com/
https://twitter.com/
https://google.com/
https://youtube.com/
https://linkedin.com/
https://instagram.com/
https://pinterest.com/

最佳答案

get_website_status 例程委托(delegate)调用 fetch 函数,该函数返回文本内容 response.text(),而不是 response 本身。
这就是为什么 response.status 会抛出一个明显的错误。

如果不需要响应内容,要修复错误,请更改 fetch 函数以返回 response 对象:

async def fetch(session, url):
response = await session.get(url)
return response

关于python - 使用aiohttp获取多个网站的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58659725/

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