rohit vyawahare
rohit vyawahare

Reputation: 85

TypeError: Can't convert 'bytes' object to str implicitly for tweepy

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

ckey=''
csecret=''
atoken=''
asecret=''

class listener(StreamListener):

    def on_data(self,data):
        print(data)
        return True

    def on_error(self,status):
        print(status)


auth = OAuthHandler(ckey,csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track="cricket")

This code filter the twitter stream based on the filter. But I am getting following traceback after running the code. Can somebody please help

Traceback (most recent call last):
  File "lab.py", line 23, in <module>
    twitterStream.filter(track="car".strip())
  File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 430, in filter
    self._start(async)
  File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 346, in _start
    self._run()
  File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 286, in _run
    raise exception
  File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 255, in _run
    self._read_loop(resp)
  File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 298, in _read_loop
    line = buf.read_line().strip()
  File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 171, in read_line
self._buffer += self._stream.read(self._chunk_size)
TypeError: Can't convert 'bytes' object to str implicitly

Upvotes: 0

Views: 1070

Answers (2)

Ayoub
Ayoub

Reputation: 101

Im assuming you're using tweepy 3.4.0. The issue you've raised is 'open' on github (https://github.com/tweepy/tweepy/issues/615).

Two work-arounds :

1) In streaming.py:

I changed line 161 to

self._buffer += self._stream.read(read_len).decode('UTF-8', 'ignore')

and line 171 to

self._buffer += self._stream.read(self._chunk_size).decode('UTF-8', 'ignore')

and then reinstalled via python3 setup.py install on my local copy of tweepy.

2) remove the tweepy 3.4.0 module, and install 3.3.0 using command: pip install -I tweepy==3.3.0

Hope that helps,

-A

Upvotes: 1

Leb
Leb

Reputation: 15953

You can't do twitterStream.filter(track="car".strip()). Why are you adding the strip() it's serving no purpose in there.

track must be a str type before you invoke a connection to Twitter's Streaming API and tweepy is preventing that connection because you're trying to add strip()

If for some reason you need it, you can do track_word='car'.strip() then track=track_word, that's even unnecessary because:

>>> print('car'.strip())
car

Also, the error you're getting does not match the code you have listed, the code that's in your question should work fine.

Upvotes: 0

Related Questions