我遵循这个教程,我被困在Tweepy安装。我按照Tweepy的github页面的说明运行sudo pip install tweepy
,安装成功:
User-MBP:Mercuria user$ sudo pip install tweepy
Downloading/unpacking tweepy
Downloading tweepy-3.4.0-py2.py3-none-any.whl
Downloading/unpacking requests>=2.4.3 (from tweepy)
Downloading requests-2.7.0-py2.py3-none-any.whl (470kB): 470kB downloaded
Downloading/unpacking six>=1.7.3 (from tweepy)
Downloading six-1.9.0-py2.py3-none-any.whl
Downloading/unpacking requests-oauthlib>=0.4.1 (from tweepy)
Downloading requests_oauthlib-0.5.0-py2.py3-none-any.whl
Downloading/unpacking oauthlib>=0.6.2 (from requests-oauthlib>=0.4.1->tweepy)
Downloading oauthlib-1.0.3.tar.gz (109kB): 109kB downloaded
Running setup.py (path:/private/tmp/pip_build_root/oauthlib/setup.py) egg_info for package oauthlib
Installing collected packages: tweepy, requests, six, requests-oauthlib, oauthlib
Found existing installation: requests 2.1.0
Uninstalling requests:
Successfully uninstalled requests
Found existing installation: requests-oauthlib 0.4.0
Uninstalling requests-oauthlib:
Successfully uninstalled requests-oauthlib
Found existing installation: oauthlib 0.6.1
Uninstalling oauthlib:
Successfully uninstalled oauthlib
Running setup.py install for oauthlib
Successfully installed tweepy requests six requests-oauthlib oauthlib
Cleaning up...
User-MBP:Mercuria user$ python feed.py
Traceback (most recent call last):
File "feed.py", line 2, in <module>
from tweepy.streaming import StreamListener
ImportError: No module named tweepy.streaming
我正在运行的代码可以在教程页面中找到,也粘贴在这里:
#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
#Variables that contains the user credentials to access Twitter API
access_token = "ENTER YOUR ACCESS TOKEN"
access_token_secret = "ENTER YOUR ACCESS TOKEN SECRET"
consumer_key = "ENTER YOUR API KEY"
consumer_secret = "ENTER YOUR API SECRET"
#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
if __name__ == '__main__':
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
#This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
stream.filter(track=['python', 'javascript', 'ruby'])
有同样的问题。对我来说,这是因为我的系统上安装了多个版本的python。我不得不做以下修复:对于v2.7
pip install tweepy
为v3.0 pip3 install tweepy
v3.5 :
pip3.5 install tweepy
我猜你想尝试使用pip版本的python版本。如果你有python 2.7.7,只需运行sudo pip2.7 install tweepy它在python 2.7.6中为我工作。
>>> from tweepy.streaming import StreamListener
>>> dir(StreamListener)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'keep_alive', 'on_connect', 'on_data', 'on_delete', 'on_direct_message', 'on_disconnect', 'on_error', 'on_event', 'on_exception', 'on_friends', 'on_limit', 'on_status', 'on_timeout', 'on_warning']
你为什么要试着从tweepy。流媒体而不仅仅是微博?
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
>>> import tweepy
>>> from tweepy import StreamListener
>>> dir(StreamListener)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'keep_alive', 'on_connect', 'on_data', 'on_delete', 'on_direct_message', 'on_disconnect', 'on_error', 'on_event', 'on_exception', 'on_friends', 'on_limit', 'on_status', 'on_timeout', 'on_warning']
>>>
StreamListener
是tweepy
包中的一个类,所以您需要从tweepy
而不是从tweepy.stream
导入它,使用正确的导入修改您的代码。
第2行
from tweepy import StreamListener
也使用IDE (PyCharm首选),这样您就不会在编码时犯这些小错误。