Trouble with Twitter API using Python

2019-02-16 00:59发布

问题:

I'm having some trouble in this python code:

import twitter
twitter_search = twitter.Twitter(domain="search.twitter.com")
trends = twitter_search.trends()

The error (404 page not found) is right here:

I'm using this package: http://github.com/sixohsix/twitter

回答1:

According to the Twitter API documentation for the trends call the domain should be api.twitter.com:

import twitter
twitter_search = twitter.Twitter(domain="api.twitter.com")
print twitter_search.trends()

{u'trends': [{u'url': u'http://search.twitter.com/search?q=%23weedcommandments', 
 u'name': u'#weedcommandments'}, ...


回答2:

Because of the new API change, you need to use this program:

import twitter
twitter_api = twitter.Twitter(domain="api.twitter.com", api_version='1')
WORLD_WOE_ID = 1
world_trends = twitter_api.trends._(WORLD_WOE_ID) 
trends = world_trends()
print [trend['name'] for trend in trends[0]['trends']]

This is based, in part, on the book errata website.



回答3:

You may want to checkout the updated IPython Notebook for Chapter 1 of Mining the Social Web that shows an updated example for this workflow and API call as well as a little bit of other context that is compatible with Twitter's v1.1 API. See http://nbviewer.ipython.org/urls/raw.github.com/ptwobrussell/Mining-the-Social-Web/master/ipython_notebooks/Chapter1.ipynb for a read-only version of the notebook.