如何使用 python 在 Twitter 上使用主题标签?
谁能告诉我如何在 Python 中使用从 Twitter 检索主题标签?我尝试过使用:
test = api.GetUserTimeline()
for i in test:
text = i.text
hashtag = i.hashtags
print text
print "\n" + hashtag
它返回 hashtag
作为 None
,而在文本中它是“life is #wonderful”
Can anyone tell me how to use retrieve hashtags from Twitter in Python? I have tried using:
test = api.GetUserTimeline()
for i in test:
text = i.text
hashtag = i.hashtags
print text
print "\n" + hashtag
It returns hashtag
as None
, while in the text it is "life is #wonderful"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,底层的 Twitter API 实际上并没有为你解析主题标签,python-twitter 包装器也没有。 (当然,我并没有很努力地寻找;))
幸运的是,您自己可以很容易地做到这一点。主题标签是以“#”开头的单词;因此,我们向 Python 询问每个以“#”开头的单词,并删除“#”(即从第二个字符到单词末尾),这些单词来自将文本拆分为单词。 Python 语言几乎比英语语言更清晰:
As far as I can tell, the underlying Twitter API doesn't actually parse out hashtags for you, and neither does the python-twitter wrapper. (Of course, I didn't look very hard ;))
Fortunately, it's pretty easy to do this yourself. Hashtags are words that start with '#'; so we ask Python for each word that starts with '#', with that '#' removed (i.e., from the second character to the end of the word), where the words come from splitting the text into words. It's almost clearer in Python than in English: