|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
想用异步爬取 www.kuaidaili.com 的免费代理ip 和一个漫画网站的所有漫画,但是每次运行程序,爬了几条之后就被封了,请问大佬要怎么调整发送频率? 这是代码
- async def get_ip(session,page):
- #访问目标网页获取html信息
- url = f'https://www.kuaidaili.com/free/inha/{page}/'
- async with session.get(url) as resp:
- text = await resp.text()
- #解析信息
- element = etree.HTML(text)
- parse_list = element.xpath('//*[@id="list"]/table/tbody/tr')
- ip_list = []
- for parse in parse_list:
- dic = {}
- ip_name = parse.xpath('./td[1]/text()')[0]
- port_name = parse.xpath('./td[2]/text()')[0]
- tpye_name = parse.xpath('./td[4]/text()')[0]
- dic[tpye_name] = ip_name + ':' + port_name
- ip_list.append(dic)
- async def main():
- async with aiohttp.ClientSession() as session:
- task = [asyncio.create_task(get_ip(session,index)) for index in range(1000)]
- await asyncio.wait(task)
- if __name__ == '__main__':
- asyncio.get_event_loop().run_until_complete(main())
复制代码
1、代理池。池中代理可用数量不足,则再换过可用代理
2、加延迟。不过会降低效率
3、每次爬取更换IP(代理)和浏览器UA标志是最优的办法
|
|