Python Extract And Link Hashtag

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.

Install.

pip install twitter-text-py

Code.

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

Output.

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>

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.