我一直在尝试使用 Twitter API,因为我想在一个特殊页面上显示一些推文列表。
在这些列表中有一个列表,其中包含包含特定主题标签的所有推文(例如#test)
但是我找不到如何在 XML 或 JSON(最好是后者)中获取该列表,有人知道如何吗?如果可以在 TweetSharp 中完成也可以
我一直在尝试使用 Twitter API,因为我想在一个特殊页面上显示一些推文列表。
在这些列表中有一个列表,其中包含包含特定主题标签的所有推文(例如#test)
但是我找不到如何在 XML 或 JSON(最好是后者)中获取该列表,有人知道如何吗?如果可以在 TweetSharp 中完成也可以
您可以简单地获取http://search.twitter.com/search.json?q=%23test
以获取包含#test
在 JSON 中的推文列表,其中%23test
URL#test
编码。
我对 TweetSharp 不熟悉,但我想一定有一个search
命令可以用来搜索#test
,然后自己将生成的推文转换为 JSON。
首先使用 github https://github.com/danielcrenna/tweetsharp安装 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 { }
}
}