我一直在嘗試一次檢查所有這些子域的狀態,并且我嘗試了多種技術,甚至 grequests 和比請求更快的技術并沒有太大幫助,然后我開始使用 asyncio 和 aiohttp,它現在比普通請求庫慢。我還檢查了它實際上并沒有異步發送請求,而是一個接一個地發送。
我知道“await resp.status”有問題,因為 resp.status 不支持 await,但我嘗試洗掉它,它仍然是一樣的。
import aiohttp
import asyncio
import time
start_time = time.time()
async def main():
#List of 1000 subdomains , Some subdomains do not exist
data = [ "LIST OF 1000 SUBDOMAINS" ]
async with aiohttp.ClientSession() as session:
for url in data:
pokemon_url = f'{url}'
try:
async with session.get(pokemon_url, ssl=False) as resp:
pokemon = await resp.status
#If subdomain exists then print the status
print(pokemon)
except:
#else print the subdomain which does not exist or cannot be reached
print(url)
asyncio.run(main())
print("--- %s seconds ---" % (time.time() - start_time))
uj5u.com熱心網友回復:
我嘗試了多種技術,甚至是 grequests
grequests對此效果很好,如果您不想要,您不必使用異步。
import grequests
import time
urls = ['https://httpbin.org/delay/4' for _ in range(4)]
# each of these requests take 4 seconds to complete
# serially, these would take at least 16 (4 * 4) seconds to complete
reqs = [grequests.get(url) for url in urls]
start = time.time()
for resp in grequests.imap(reqs, size=4):
print(resp.status_code)
end = time.time()
print('finished in', round(end-start, 2), 'seconds')
200
200
200
200
finished in 4.32 seconds
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/442716.html
