Tweepy / Python hängt
Verfasst: Freitag 1. Mai 2020, 08:02
Hallo, ich möchte einen Twitter-Bot programmieren. Bei meinem Script wird aber nicht ausgeführt. Das Problem sehe ich leider nicht
Code:
Ich nutze ubuntu. Wenn ich den Prozess abbreche, kommt folgender Hinweis:
^CTraceback (most recent call last):
File "tweepy_streamer.py", line 58, in <module>
twitter_streamer.stream_tweets(fetched_tweets_filename, hash_tag_list)
File "tweepy_streamer.py", line 25, in stream_tweets
stream.filter(track=hash_tag_list)
File "/home/admin/.local/lib/python3.6/site-packages/tweepy/streaming.py", line 474, in filter
self._start(is_async)
File "/home/admin/.local/lib/python3.6/site-packages/tweepy/streaming.py", line 389, in _start
self._run()
File "/home/admin/.local/lib/python3.6/site-packages/tweepy/streaming.py", line 289, in _run
self._read_loop(resp)
File "/home/admin/.local/lib/python3.6/site-packages/tweepy/streaming.py", line 339, in _read_loop
line = buf.read_line()
File "/home/admin/.local/lib/python3.6/site-packages/tweepy/streaming.py", line 200, in read_line
self._buffer += self._stream.read(self._chunk_size)
File "/home/admin/.local/lib/python3.6/site-packages/urllib3/response.py", line 444, in read
data = self._fp.read(amt)
File "/usr/lib/python3.6/http/client.py", line 459, in read
n = self.readinto(b)
File "/usr/lib/python3.6/http/client.py", line 493, in readinto
return self._readinto_chunked(b)
File "/usr/lib/python3.6/http/client.py", line 588, in _readinto_chunked
chunk_left = self._get_chunk_left()
File "/usr/lib/python3.6/http/client.py", line 556, in _get_chunk_left
chunk_left = self._read_next_chunk_size()
File "/usr/lib/python3.6/http/client.py", line 516, in _read_next_chunk_size
line = self.fp.readline(_MAXLINE + 1)
File "/usr/lib/python3.6/socket.py", line 586, in readinto
return self._sock.recv_into(b)
File "/usr/lib/python3.6/ssl.py", line 1012, in recv_into
return self.read(nbytes, buffer)
File "/usr/lib/python3.6/ssl.py", line 874, in read
return self._sslobj.read(len, buffer)
File "/usr/lib/python3.6/ssl.py", line 631, in read
v = self._sslobj.read(len, buffer)
Mit einem anderen Script funtioniert es:
kann mir da jmd auf die Sprünge helfen?
Code:
Code: Alles auswählen
#!/usr/bin/python
# -*- coding: utf-8 -*-
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import twitter_credentials
# # # # TWITTER STREAMER # # # #
class TwitterStreamer():
"""
Class for streaming and processing live tweets.
"""
def __init__(self):
pass
def stream_tweets(self, fetched_tweets_filename, hash_tag_list):
# This handles Twitter authetification and the connection to Twitter Streaming API
listener = StdOutListener(fetched_tweets_filename)
auth = OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET)
auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET)
stream = Stream(auth, listener)
# This line filter Twitter Streams to capture data by the keywords:
stream.filter(track=hash_tag_list)
# # # # TWITTER STREAM LISTENER # # # #
class StdOutListener(StreamListener):
"""
This is a basic listener that just prints received tweets to stdout.
"""
def __init__(self, fetched_tweets_filename):
self.fetched_tweets_filename = fetched_tweets_filename
def on_data(self, data):
try:
print(data)
with open(self.fetched_tweets_filename, 'a') as tf:
tf.write(data)
return True
except BaseException as e:
print("Error on_data %s" % str(e))
return True
def on_error(self, status):
print(status)
if __name__ == '__main__':
# Authenticate using config.py and connect to Twitter Streaming API.
hash_tag_list = ["#besserbeideeltern"]
fetched_tweets_filename = "tweets.txt"
twitter_streamer = TwitterStreamer()
twitter_streamer.stream_tweets(fetched_tweets_filename, hash_tag_list)
^CTraceback (most recent call last):
File "tweepy_streamer.py", line 58, in <module>
twitter_streamer.stream_tweets(fetched_tweets_filename, hash_tag_list)
File "tweepy_streamer.py", line 25, in stream_tweets
stream.filter(track=hash_tag_list)
File "/home/admin/.local/lib/python3.6/site-packages/tweepy/streaming.py", line 474, in filter
self._start(is_async)
File "/home/admin/.local/lib/python3.6/site-packages/tweepy/streaming.py", line 389, in _start
self._run()
File "/home/admin/.local/lib/python3.6/site-packages/tweepy/streaming.py", line 289, in _run
self._read_loop(resp)
File "/home/admin/.local/lib/python3.6/site-packages/tweepy/streaming.py", line 339, in _read_loop
line = buf.read_line()
File "/home/admin/.local/lib/python3.6/site-packages/tweepy/streaming.py", line 200, in read_line
self._buffer += self._stream.read(self._chunk_size)
File "/home/admin/.local/lib/python3.6/site-packages/urllib3/response.py", line 444, in read
data = self._fp.read(amt)
File "/usr/lib/python3.6/http/client.py", line 459, in read
n = self.readinto(b)
File "/usr/lib/python3.6/http/client.py", line 493, in readinto
return self._readinto_chunked(b)
File "/usr/lib/python3.6/http/client.py", line 588, in _readinto_chunked
chunk_left = self._get_chunk_left()
File "/usr/lib/python3.6/http/client.py", line 556, in _get_chunk_left
chunk_left = self._read_next_chunk_size()
File "/usr/lib/python3.6/http/client.py", line 516, in _read_next_chunk_size
line = self.fp.readline(_MAXLINE + 1)
File "/usr/lib/python3.6/socket.py", line 586, in readinto
return self._sock.recv_into(b)
File "/usr/lib/python3.6/ssl.py", line 1012, in recv_into
return self.read(nbytes, buffer)
File "/usr/lib/python3.6/ssl.py", line 874, in read
return self._sslobj.read(len, buffer)
File "/usr/lib/python3.6/ssl.py", line 631, in read
v = self._sslobj.read(len, buffer)
Mit einem anderen Script funtioniert es:
Code: Alles auswählen
import tweepy
#Add your credentials here
twitter_keys = {
'consumer_key': '',
'consumer_secret': '',
'access_token_key': '',
'access_token_secret': '
}
#Setup access to API
auth = tweepy.OAuthHandler(twitter_keys['consumer_key'], twitter_keys['consumer_secret'])
auth.set_access_token(twitter_keys['access_token_key'], twitter_keys['access_token_secret'])
api = tweepy.API(auth)
#Make call on home timeline, print each tweets text
public_tweets = api.home_timeline()
for tweet in public_tweets:
print(tweet.text)