V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
xinali
V2EX  ›  Python

python 的一段代码解释

  •  
  •   xinali · 2016-07-15 16:08:46 +08:00 · 3111 次点击
    这是一个创建于 2845 天前的主题,其中的信息可能已经有所发展或是发生改变。
    def check_spider_middleware(method):
    @functools.wraps(method)
    def wrapper(self, request, spider):
        msg = '%%s %s middleware step' % (self.__class__.__name__,)
        if self.__class__ in spider.middleware:
            spider.log(msg % 'executing', level=log.DEBUG)
            return method(self, request, spider)
        else:
            spider.log(msg % 'skipping', level=log.DEBUG)
            return None
    
    return wrapper
    
    

    代码的出处

    主要是这里没看懂,函数可以这么定义吗

    
    def check_spider_middleware(method):
    @functools.wraps(method)
    def wrapper(self, request, spider):
    
    

    这段代码的上面还有一段这个代码

    
    class JsDownload(object):
    
    @check_spider_middleware
    def process_request(self, request, spider):
        driver = webdriver.PhantomJS(executable_path='D:\phantomjs.exe')
        driver.get(request.url)
        return HtmlResponse(request.url, encoding='utf-8', body=driver.page_source.encode('utf-8'))
        
    
    4 条回复    2016-07-15 16:35:42 +08:00
    alioth310
        1
    alioth310  
       2016-07-15 16:10:51 +08:00   ❤️ 1
    缩进丢了
    deanguqiang
        2
    deanguqiang  
       2016-07-15 16:14:53 +08:00 via iPhone
    搜索 装饰器
    raycool
        3
    raycool  
       2016-07-15 16:17:48 +08:00   ❤️ 1
    quxw
        4
    quxw  
       2016-07-15 16:35:42 +08:00
    见 1 , 2 楼
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5845 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 01:55 · PVG 09:55 · LAX 18:55 · JFK 21:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.