异步协程爬虫模板

import re
import time
import json
import aiohttp
import asyncio
import execjs
import requests
from fake_useragent import UserAgent
import nest_asyncio

nest_asyncio.apply()


class Spider:
    def __init__(self):
        self.session = None
        # 登录获得session

    # 运行程序方法
    def run(self):
        task = [asyncio.ensure_future(self.main())]
        loop = asyncio.get_event_loop()
        data = loop.run_until_complete(asyncio.gather(*task))[0]
        return data

    # 同步登录获得session
    def login(self, username, password, **kwargs):
        headers = {
            'User-Agent': UserAgent().random,
        }
        self.session = requests.post()
        pass

    # 异步爬虫主程序
    async def main(self, url=None):
        # session = self.login(username, password)
        # 将登录获得session的cookie添加至异步会话中
        cookies = requests.utils.dict_from_cookiejar(self.session.cookies)
        async with aiohttp.ClientSession(cookies=cookies) as session:
            data = await self.post(url, session)
            # 嵌套异步请求
            await self.get_data(session)
            return data

    # 异步方法获得数据
    async def get_data(self, session=None):
        # 往task中添加发起请求函数
        task = [asyncio.ensure_future(function=None)]
        loop = asyncio.get_event_loop()
        data = loop.run_until_complete(asyncio.gather(*task))
        return data

    # 定义aiohttp异步get和post方法
    async def get(self, url, session, headers=None):
        async with session.get(url, headers=headers) as r:
            return await r.text()

    async def post(self, url, session, headers=None, json=None):
        async with session.post(url, headers=headers, json=json) as r:
            if r.status == 200:
                return await r.json()
            else:
                # print(r.status, r.text())
                return {'code': r.status, 'msg': r}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值