Get all tweets with specific hashtag

2019-02-13 16:45发布

I've been experimenting with the Twitter API because I want to display a few lists of tweets on a special page.

Among those lists is a list with all tweets containing a specific hashtag (e.g. #test)

However I cannot find how to get that list in either XML or JSON (preferably the latter), does anyone know how? It is also fine if it can be done in TweetSharp

5条回答
放我归山
2楼-- · 2019-02-13 17:15

You can simply fetch http://search.twitter.com/search.json?q=%23test to get a list of tweets containing #test in JSON, where %23test is #test URL encoded.

I'm not familiar with TweetSharp, but I guess there must be a search command that you can use to search for #test, and then transform the resulting tweets into JSON yourself.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-02-13 17:21

First install TweetSharp using github https://github.com/danielcrenna/tweetsharp

Here is the code to do a search

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

If you have more then one page results you can setup a loop and call each page

 service.Search("#Test", i += 1, 100);
查看更多
Explosion°爆炸
4楼-- · 2019-02-13 17:22

It seems like there is a change in the API since last few months. Here is the updated code:

TwitterSearchResult res = twitter.Search(new SearchOptions { Q = "xbox" });
IEnumerable<TwitterStatus> status = res.Statuses;
查看更多
你好瞎i
5楼-- · 2019-02-13 17:30

u access with this url for your tweet searchs. But u have to use OAuth protocols.

https://api.twitter.com/1.1/search/tweets.json?q=%40twitterapi

查看更多
甜甜的少女心
6楼-- · 2019-02-13 17:31

I struggled with the same problem. Here is my vague solution . Enjoy Programming. It will get out of the function whenever your required number of tweets are acquired/fetched.

        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 { }
                }

}

查看更多
登录 后发表回答