GithubHelp home page GithubHelp logo

aiolearn's Introduction

license Build Status

aiolearn

Thu Learn Spider for asyncio (PEP-3156)

关于这个项目

之前我完成了thu_learn项目作为清华大学的网络爬虫,但是效率一直是一个问题。 在那个项目中我使用的是requests作为网络库,请求效率虽然不慢,但是由于是阻塞的,会浪费大量的时间在网络等待上。 在这个项目中,我使用了python3.5的asyncio语法换用了异步的aiohttp库作为网络请求,使得爬虫的大部分行为变为异步的。 同时更改了类的结构,将用户及其session作为了类的一部分,使之可以同时处理多用户连接。 由于异步地重构使得库的使用方法大大改变,必须使用异步的方法进行调用,因此有了这个全新的项目。

性能对比图

简单理解 asynico:

通过 async def 来声明的函数为协程函数。协程函数的执行非同步,而是在异步执行,当遇见IO block的时候,如在本项目的网络请求,可以近乎并行执行。 协程需要使用asynico的event.loop来包裹执行。

示例使用

import asyncio
import aiolearn
import getpass

async def main():
    user = aiolearn.User(username=input('input username'), password=getpass.getpass("input password:"))
    semester = aiolearn.Semester(user, current=False)
    courses = await semester.courses
    for course in courses:
        print(course.name)
        works = await course.works
        messages = await course.messages
        files = await course.files
        print('\n>>works')
        for work in works:
            print(work.title)
        print('\n>>messages')
        for message in messages:
            print(message.title)
        print('\n>>files')
        for file in files:
            print(file.name)

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    

semester.courses即协程对象,需要使用await代表获得其值。

aiolearn's People

Contributors

kehao95 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.