获取特定标签的所有推文

6
我一直在尝试使用Twitter API,因为我想在一个特殊的页面上显示几个推文列表。
其中之一是包含特定标签(例如#test)的所有推文列表。
然而,我无法找到如何以XML或JSON格式(最好是后者)获取该列表的方法。有人知道吗?如果可以在TweetSharp中完成也可以。
5个回答

9
您可以通过获取http://search.twitter.com/search.json?q=%23test来获取包含#test的推文列表,其中%23test#test的URL编码。
我不熟悉TweetSharp,但我认为必须有一个search命令可用于搜索#test,然后自己将结果转换为JSON。

很遗憾,服务(search.twitter.com)似乎经常出现故障 :( 是否有使用api.twitter.com的替代方案? - dtech
2
不再工作了...不要在这里浪费时间。 - kumar kundan
1
这是使用旧的 API(v1) 。对于新的 API(v1.1),请访问:https://dev.twitter.com/rest/reference/get/search/tweets。其余部分仍然适用。在这种情况下,您将使用的 URL 是:https://api.twitter.com/1.1/search/tweets.json?q=%23test,但现在您需要使用 oAuth 对请求进行身份验证。 - hammygoonan

8

首先使用Github安装TweetSharp:https://github.com/danielcrenna/tweetsharp

以下是进行搜索的代码:

TwitterService service = new TwitterService();
var tweets = service.Search("#Test", 100);
List<TwitterSearchStatus> resultList = new List<TwitterSearchStatus>(tweets.Statuses);    

如果您有多个页面结果,可以设置循环并调用每个页面。
 service.Search("#Test", i += 1, 100);

4

最近几个月API似乎有所变化。这里是更新后的代码:

TwitterSearchResult res = twitter.Search(new SearchOptions { Q = "xbox" });
IEnumerable<TwitterStatus> status = res.Statuses;

0
我曾经也遇到过同样的问题。这是我的模糊解决方案。享受编程吧。 当获取到所需数量的推文时,它将退出函数。
        string maxid = "1000000000000"; // dummy value
        int tweetcount = 0;


        if (maxid != null)
        {
            var tweets_search = twitterService.Search(new SearchOptions { Q = keyword, Count = Convert.ToInt32(count) });
            List<TwitterStatus> resultList = new List<TwitterStatus>(tweets_search.Statuses);
            maxid = resultList.Last().IdStr;
            foreach (var tweet in tweets_search.Statuses)
            {
                try
                {
                    ResultSearch.Add(new KeyValuePair<String, String>(tweet.Id.ToString(), tweet.Text));
                    tweetcount++;
                }
                catch { }
            }

            while (maxid != null && tweetcount < Convert.ToInt32(count))
            {
                maxid = resultList.Last().IdStr;
                tweets_search = twitterService.Search(new SearchOptions { Q = keyword, Count = Convert.ToInt32(count), MaxId = Convert.ToInt64(maxid) });
                resultList = new List<TwitterStatus>(tweets_search.Statuses);
                foreach (var tweet in tweets_search.Statuses)
                {
                    try
                    {
                        ResultSearch.Add(new KeyValuePair<String, String>(tweet.Id.ToString(), tweet.Text));
                        tweetcount++;
                    }
                    catch { }
                }

}


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接