Ошибка «Только объекты Unicode могут быть сброшены» при попытке выполнить вызов API для Twitter - PullRequest
0 голосов
/ 06 апреля 2020

У меня возникли проблемы с подключением к API Twitter. Я следую указаниям в этой статье . Однако, когда я пытаюсь подключиться к API, я обнаруживаю эту ошибку в самой последней строке:

stream.filter(track = track, languages = ['en'])

ValueError: Only unicode objects are escapable. Got <generator object to_unicode.<locals>.<genexpr> at 0x7f8f70928048> of type <class 'generator'>.

Я понятия не имею, как обойти это. Любая помощь приветствуется, я приложил код, с которым работаю ниже.

    import mysql.connector
from mysql.connector import Error
import tweepy
import json
from dateutil import parser
import time
import os
import subprocess

#importing file which sets env variable
subprocess.call("./settings.sh", shell = True)


consumer_key = ['xxxxxxxxxxx']
consumer_secret = ['xxxxxxxxxxxxxxx']
access_token = ['xxxxxxxxxxxxxxxxxxxxx']
access_token_secret = ['xxxxxxxxxxxxxxxxxxxx']
password = ['xxxxxxxxxxxxxxxxx']


def connect(username, created_at, tweet, retweet_count, place , location):
    """
    connect to MySQL database and insert twitter data
    """
    try:
        con = mysql.connector.connect(host = 'localhost',
        database='twitterdb', user='root', password = password, charset = 'utf8')


        if con.is_connected():
            """
            Insert twitter data
            """
            cursor = con.cursor()
            # twitter, golf
            query = "INSERT INTO tweet_data (username, created_at, tweet, retweet_count, location, place) VALUES (%s, %s, %s, %s, %s, %s)"
            cursor.execute(query, (username, created_at, tweet, retweet_count, location, place))
            con.commit()


    except Error as e:
        print(e)

    cursor.close()
    con.close()

    return


# Tweepy class to access Twitter API
class Streamlistener(tweepy.StreamListener):


    def on_connect(self):
        print("You are connected to the Twitter API")


    def on_error(self):
        if status_code != 200:
            print("error found")
            # returning false disconnects the stream
            return False

    """
    This method reads in tweet data as Json
    and extracts the data we want.
    """
    def on_data(self,data):

        try:
            raw_data = json.loads(data)

            if 'text' in raw_data:

                username = raw_data['user']['screen_name']
                created_at = parser.parse(raw_data['created_at'])
                tweet = raw_data['text']
                retweet_count = raw_data['retweet_count']

                if raw_data['place'] is not None:
                    place = raw_data['place']['country']
                    print(place)
                else:
                    place = None


                location = raw_data['user']['location']

                #insert data just collected into MySQL database
                connect(username, created_at, tweet, retweet_count, place, location)
                print("Tweet colleted at: {} ".format(str(created_at)))
        except Error as e:
            print(e)


if __name__== '__main__':

    # # #Allow user input
    # track = []
    # while True:

    #   input1  = input("what do you want to collect tweets on?: ")
    #   track.append(input1)

    #   input2 = input("Do you wish to enter another word? y/n ")
    #   if input2 == 'n' or input2 == 'N':
    #       break

    # print("You want to search for {}".format(track))
    # print("Initialising Connection to Twitter API....")
    # time.sleep(2)

    # authentification so we can access twitter
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth, wait_on_rate_limit=True)

    # create instance of Streamlistener
    listener = Streamlistener(api = api)
    stream = tweepy.Stream(auth, listener = listener)

    track = ['word1','word2','word3','word4','word5']
    # choose what we want to filter by
    stream.filter(track = track, languages = ['en'])
...