>>> import twitter
>>> twitter_search = twitter.Twitter(domain="search.twitter.com")
>>> trends = twitter_search.trends()
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
trends = twitter_search.trends()
File "build\bdist.win32\egg\twitter\api.py", line 173, in __call__
return self._handle_response(req, uri, arg_data)
File "build\bdist.win32\egg\twitter\api.py", line 198, in _handle_response
raise TwitterHTTPError(e, uri, self.format, arg_data)
TwitterHTTPError: Twitter sent status 404 for URL: trends.json using parameters: ()
details: ‹
我也是通过改变域名
来尝试的>>> 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)
>>> [ trend for trend in world_trends()[0]['trends'] ]
[{u'url': u'http://twitter.com/search/?q=%2320CutestFollowers', u'query': u'%2320CutestFollowers', u'events': None, u'promoted_content': None, u'name': u'#20CutestFollowers'}, {u'url': u'http://twitter.com/search/?q=%23watkiestnl', u'query': u'%23watkiestnl', u'events': None, u'promoted_content': None, u'name': u'#watkiestnl'}, {u'url': u'http://twitter.com/search/?q=%23FelizCumplea%C3%B1osAmaia', u'query': u'%23FelizCumplea%C3%B1osAmaia', u'name': u'#FelizCumplea\xf1osAmaia', u'promoted_content': None, u'events': None}, {u'url': u'http://twitter.com/search/?q=%22Hi%20Boyfriend%22', u'query': u'%22Hi%20Boyfriend%22', u'events': None, u'promoted_content': None, u'name': u'Hi Boyfriend'}, {u'url': u'http://twitter.com/search/?q=%22Christina%20Legendtina%20Aguilera%22', u'query': u'%22Christina%20Legendtina%20Aguilera%22', u'events': None, u'promoted_content': None, u'name': u'Christina Legendtina Aguilera'}, {u'url': u'http://twitter.com/search/?q=%22Jeroen%20Pauw%22', u'query': u'%22Jeroen%20Pauw%22', u'events': None, u'promoted_content': None, u'name': u'Jeroen Pauw'}, {u'url': u'http://twitter.com/search/?q=%22Denmark%20Has%20%C3%86bleskiver%20Niall%22', u'query': u'%22Denmark%20Has%20%C3%86bleskiver%20Niall%22', u'name': u'Denmark Has \xc6bleskiver Niall', u'promoted_content': None, u'events': None}, {u'url': u'http://twitter.com/search/?q=Jide', u'query': u'Jide', u'events': None, u'promoted_content': None, u'name': u'Jide'}, {u'url': u'http://twitter.com/search/?q=T.O', u'query': u'T.O', u'events': None, u'promoted_content': None, u'name': u'T.O'}, {u'url': u'http://twitter.com/search/?q=Getafe', u'query': u'Getafe', u'events': None, u'promoted_content': None, u'name': u'Getafe'}]
我得到了结果,但我无法理解。我是研究工作的新手,也是python和twitterApi的新手。
答案 0 :(得分:0)
如前所述,但将列表理解分配给trends
:
>>> 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 = [ trend for trend in world_trends()[0]['trends'] ]
trends
只是一个字典列表(您可以使用pprint.pprint
打印这样的结构,以便更容易阅读)
要获取趋势名称,您只需访问每个字典中的name
项,如下所示:
>>> [cur['name'] for cur in trends]
[u'#20CutestFollowers', u'#watkiestnl', ...]
或者:
for cur in trends:
print cur['name']