Get all tweets with specific hashtag

2019-02-13 16:35发布

问题:

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

回答1:

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.



回答2:

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);


回答3:

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;


回答4:

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



回答5:

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

}