【问题标题】:how to retrieve the first hashtag of a tweet in Python?如何在 Python 中检索推文的第一个主题标签?
【发布时间】:2017-02-18 09:19:51
【问题描述】:

作为标题,在 Python 中检索推文的第一个主题标签的代码是什么?

将输出标记为字符串 tag= for tag in tw.split() if tag.startswith("#")}

【问题讨论】:

  • [tag for tag in tw.split() if tag.startswith("#")][0].

标签: python twitter


【解决方案1】:

使用正则表达式 ...

有点像

>>> tweet = 'some text #tagtag #anothertag'
>>> import re
>>> re.findall(r'\B\#\w+\b', tweet)
['#tagtag', '#anothertag']

使用https://www.regex101.com 帮助您调整正则表达式

【讨论】:

    猜你喜欢
    • 2010-10-31
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多