码迷,mamicode.com
首页 > 编程语言 > 详细

python-自定义异步非阻塞爬虫框架

时间:2018-10-20 11:49:44      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:htm   .com   name   __init__   rom   att   sel   turn   bre   

api

import socket
import select


class MySock:
    def __init__(self, sock, data):
        self.sock = sock
        self.data = data

    def __getattr__(self, item):
        return getattr(self.sock, item)


class YinBing:
    def __init__(self):
        self.r_list = []
        self.w_list = []

    def add(self, req_info):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setblocking(0)
        try:
            sock.connect((req_info.get("host"), req_info.get("port")))
        except BlockingIOError:
            pass
        s = MySock(sock, req_info)
        self.r_list.append(s)
        self.w_list.append(s)

    def run(self):
        while True:
            rl, wl, el = select.select(self.r_list, self.w_list, [], 0.5)
            for sock in wl:
                sock.send(("GET %s Http/1.1\r\nHost:%s\r\n\r\n" % (sock.data["path"], sock.data["host"])).encode("utf-8"))
            for sock in rl:
                response = sock.recv(1024)
                callback = sock.data.get("callback")
                if callback: callback(response)
                self.r_list.remove(sock)

            if not self.r_list:
                break

调用方法

from 自定义爬虫框架 import YinBing


def done1(response):
    print(‘处理一‘, response)


def done2(response):
    print(response)


url_list = [
    {‘host‘: ‘www.baidu.com‘, ‘port‘: 80, ‘path‘: ‘/‘, ‘callback‘: done1},
    {‘host‘: ‘www.cnblogs.com‘, ‘port‘: 80, ‘path‘: ‘/index.html‘, ‘callback‘: done2},
    {‘host‘: ‘www.bing.com‘, ‘port‘: 80, ‘path‘: ‘/‘, ‘callback‘: None},
]

if __name__ == ‘__main__‘:
    y = YinBing()
    for obj in url_list:
        y.add(obj)
    y.run()

python-自定义异步非阻塞爬虫框架

标签:htm   .com   name   __init__   rom   att   sel   turn   bre   

原文地址:https://www.cnblogs.com/LTEF/p/9820955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!