在Tweepy中关注用户无输出

时间:2018-11-13 20:17:22

标签: python twitter streaming tweepy username

我在Python 3.6中有代码,并且正在尝试使此代码正常工作,这并不是我遵循的用户指定的推文。我从正常运行的代码中提取了此代码的基本结构,该代码从全局twitter流中跟踪了关键字。下面的代码行由于无法接收流而无法正常运行。

stream.filter(follow=[user_id])

以下是完整的代码,仅供参考。

import settings
import tweepy
import dataset
import datetime
import time
from textblob import TextBlob
from sqlalchemy.exc import ProgrammingError
import json

print("Starting scraper.py")
db = dataset.connect(settings.CONNECTION_STRING)
print("Connected to dataset")

class StreamListener(tweepy.StreamListener):

    def  on_status(self, status):
        print(status.text)
        description = status.user.description
        loc = status.user.location
        text = status.text
        coords = status.coordinates
        geo = status.geo
        name = status.user.screen_name
        user_created = status.user.created_at
        followers = status.user.followers_count
        id_str = status.id_str
        created = status.created_at
        retweets = status.retweet_count
        bg_color = status.user.profile_background_color
        blob = TextBlob(text)
        sent = blob.sentiment

        if geo is not None:
            geo = json.dumps(geo)

        if coords is not None:
            coords = json.dumps(coords)

        table = db[settings.TABLE_NAME]
        try:
            table.insert(dict(
                user_description=description,
                user_location=loc,
                coordinates=coords,
                text=text,
                geo=geo,
                user_name=name,
                user_created=user_created,
                user_followers=followers,
                id_str=id_str,
                created=created,
                retweet_count=retweets,
                user_bg_color=bg_color,
                polarity=sent.polarity,
                subjectivity=sent.subjectivity,
            ))
        except ProgrammingError as err:
            print(err)

    def on_error(self, status_code):
        if status_code == 420:
            # returning False in on_data disconnects the stream
            return False

auth = tweepy.OAuthHandler(settings.TWITTER_APP_KEY, settings.TWITTER_APP_SECRET)
auth.set_access_token(settings.TWITTER_KEY, settings.TWITTER_SECRET)
api = tweepy.API(auth)

print("Running Stream Listener")
stream_listener = StreamListener()
stream = tweepy.Stream(auth=api.auth, listener=stream_listener)
user_id = 'ThomasC57976849'
print("Filtering Stream By User ", user_id)

ts = time.time()
#print(ts)
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
print(st)

#stream.filter(follow=['NCDOT_I85'])
#stream.filter(follow=['hwytestacct'])
stream.filter(follow=[user_id])

0 个答案:

没有答案