getattr的使用

from requests_html import HTMLSession


class UrlGenerator(object):
    def __init__(self, root_url):
        self.url = root_url
        self.session = HTMLSession()

    def __getattr__(self, item):
        if item == 'get':
            self.get_html()
        elif item == 'post':
            print("post")
        return UrlGenerator('{}.{}'.format(self.url, item))

    def get_html(self):
        req = self.session.get(self.url)
        print(req.text)


url_gen = UrlGenerator('https://www')
url_gen.baidu.com.get

  

posted @ 2018-09-11 10:18  公众号python学习开发  阅读(357)  评论(0编辑  收藏  举报