【发布时间】:2011-03-13 16:17:59
【问题描述】:
我一直在试验 Twitter API,因为我想在一个特殊页面上显示一些推文列表。
在这些列表中有一个列表,其中包含包含特定主题标签的所有推文(例如 #test)
但是我找不到如何在 XML 或 JSON(最好是后者)中获取该列表,有人知道如何吗?如果可以在TweetSharp中完成也可以
【问题讨论】:
标签: twitter tweetsharp
我一直在试验 Twitter API,因为我想在一个特殊页面上显示一些推文列表。
在这些列表中有一个列表,其中包含包含特定主题标签的所有推文(例如 #test)
但是我找不到如何在 XML 或 JSON(最好是后者)中获取该列表,有人知道如何吗?如果可以在TweetSharp中完成也可以
【问题讨论】:
标签: twitter tweetsharp
您可以简单地获取http://search.twitter.com/search.json?q=%23test 以获取包含#test 的JSON 推文列表,其中%23test 是#test URL 编码的。
我对 TweetSharp 不熟悉,但我猜肯定有一个 search 命令可以用来搜索 #test,然后自己将生成的推文转换为 JSON。
【讨论】:
首先使用 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);
【讨论】:
自过去几个月以来,API 似乎发生了变化。这是更新的代码:
TwitterSearchResult res = twitter.Search(new SearchOptions { Q = "xbox" });
IEnumerable<TwitterStatus> status = res.Statuses;
【讨论】:
您可以使用此 URL 访问您的推文搜索。但是你必须使用 OAuth 协议。
https://api.twitter.com/1.1/search/tweets.json?q=%40twitterapi
【讨论】:
我也遇到过同样的问题。这是我模糊的解决方案。享受编程。 每当您获得/获取所需数量的推文时,它就会退出该功能。
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 { }
}
}
【讨论】: