Python Extract And Link Hashtag

December 19, 2017
Using twitter-text-py

twitter-text-py is a port of twitter-text-rb, where the last release is 2014-01-16.

Note: twitter-text only officially support Java, Ruby, JavaScript and Objective-C. I didn’t find a more recent implementation of twitter-text for Python.


pip install twitter-text-py


import logging
from twitter_text import Autolink

str = u'I am #hello #My World #我爱你'
html = Autolink(str).auto_link_hashtags({'hashtag_url_base': '/t/', 'hashtag_class': 'hashtag'})


I am <a class="hashtag" href="/t/hello" title="#hello">#hello</a> <a class="hashtag" href="/t/My" title="#My">#My</a> World <a class="hashtag" href="/t/我爱你" title="#我爱你">#我爱你</a>
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.