Posted by & filed under python.

Задача: Сделать добавление nofollow ко всем внешним ссылкам.
Решение:
Сделаем через middleware

class NofollowLinkMiddleware(object):
    ''' Adds nofolow to external links'''

    def __init__(self):
        self.extlinks = re.compile(r'''<a (?P[^>]*http.?://)''')
    def process_response(self, request, response):
        if ("text" in response['Content-Type']):
            response.content = self.extlinks.sub('<a rel="nofollow" g',response.content)
            return response
        else:
            return response
Опубликовать в Facebook
Опубликовать в Google Plus

Leave a Reply

You must be logged in to post a comment.