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