Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 修改的tweepy流类_Python_Google Sheets_Tweepy_Gspread - Fatal编程技术网

Python 修改的tweepy流类

Python 修改的tweepy流类,python,google-sheets,tweepy,gspread,Python,Google Sheets,Tweepy,Gspread,我有一个项目来练习我的Python技能: 使用Tweepy流提取一些tweets坐标 将它们放入谷歌电子表格 然后使用Google电子表格在中创建地图 我已经能够独立完成所有这些事情了。现在,我们面临的挑战是让一切都能协同工作!:) 要更新我的谷歌电子表格,我正在使用 但是,要更新单元格,我需要如下指示单元格的行和列: worksheet.update_acell('B1', 'Bingo!') 我试图在我的脚本中有一个计数器来提取推文。目标是让B1在每次发现一条推文时都变为B2、B3、B4

我有一个项目来练习我的Python技能:

  • 使用Tweepy流提取一些tweets坐标
  • 将它们放入谷歌电子表格
  • 然后使用Google电子表格在中创建地图
  • 我已经能够独立完成所有这些事情了。现在,我们面临的挑战是让一切都能协同工作!:)

    要更新我的谷歌电子表格,我正在使用

    但是,要更新单元格,我需要如下指示单元格的行和列:

    worksheet.update_acell('B1', 'Bingo!')
    
    我试图在我的脚本中有一个计数器来提取推文。目标是让B1在每次发现一条推文时都变为B2、B3、B4

    但它不起作用。。。坐标打印在我的终端上,但仅此而已

    我想我没有像我应该的那样使用这个课程。但我不明白我的错误在哪里

    帮忙


    我自己测试这个时遇到了困难(主要是因为我不熟悉
    tweepy.Stream
    的工作原理,我想),但是看起来您的
    MyStream
    实例从一开始就没有设置它的
    wks
    属性

    这意味着,当您引用
    self.wks
    时,可能会引发
    AttributeError
    ,但由于您的
    try
    /
    块除外,因此您永远看不到它。(顺便说一句,这就是为什么
    除了:pass
    往往很难排除故障。)

    您可能希望使
    MyStream
    获得一个额外的
    wks
    参数,类似这样:

    def __init__(self, wks):
        tweepy.StreamListener.__init__(self)
    
        # Store the worksheet on this instance.
        self.wks = wks
    
        # I added this to have a counter.
        self.n = 2
    
    然后更改实例化
    MyStream
    的行,现在将该工作表作为参数传递:

    stream = tweepy.Stream(auth, MyStream(wks), timeout=50)
    

    我自己测试这个时遇到了困难(主要是因为我不熟悉
    tweepy.Stream
    的工作原理,我想),但是看起来您的
    MyStream
    实例从一开始就没有设置它的
    wks
    属性

    这意味着,当您引用
    self.wks
    时,可能会引发
    AttributeError
    ,但由于您的
    try
    /
    块除外,因此您永远看不到它。(顺便说一句,这就是为什么
    除了:pass
    往往很难排除故障。)

    您可能希望使
    MyStream
    获得一个额外的
    wks
    参数,类似这样:

    def __init__(self, wks):
        tweepy.StreamListener.__init__(self)
    
        # Store the worksheet on this instance.
        self.wks = wks
    
        # I added this to have a counter.
        self.n = 2
    
    然后更改实例化
    MyStream
    的行,现在将该工作表作为参数传递:

    stream = tweepy.Stream(auth, MyStream(wks), timeout=50)
    
    我找到了答案

    事实上,@jornsharpe和@myersjustinc,你们都是对的

    “wks”没有正确设置,我也没有正确使用“self”

    谢谢大家!!你的提示帮助我找到了答案

    编辑:这里是工作代码

    class MyStream(tweepy.StreamListener):
    def __init__(self):
        tweepy.StreamListener.__init__(self)
    
        # I added self wks but also the login step on the same line
        self.wks = gspread.login('EMAIL', 'PASSWORD').open('SPREADSHEET').sheet1
    
        # I added this to have a counter.
        self.n = 2
    
    def on_status(self, tweet):
        try:
            longitude = str(tweet.coordinates['coordinates'][0])
            latitude =  str(tweet.coordinates['coordinates'][1])
            print longitude
            print latitude
    
            # I added this to update my google spreadsheet with the coordinates
            self.wks.update_acell(('A' + str(self.n)), longitude)
            self.wks.update_acell(('B' + str(self.n)), latitude)
            print "Spreadsheet updated!"
    
            # This is for my counter
            self.n += 1
    
    我找到了答案

    事实上,@jornsharpe和@myersjustinc,你们都是对的

    “wks”没有正确设置,我也没有正确使用“self”

    谢谢大家!!你的提示帮助我找到了答案

    编辑:这里是工作代码

    class MyStream(tweepy.StreamListener):
    def __init__(self):
        tweepy.StreamListener.__init__(self)
    
        # I added self wks but also the login step on the same line
        self.wks = gspread.login('EMAIL', 'PASSWORD').open('SPREADSHEET').sheet1
    
        # I added this to have a counter.
        self.n = 2
    
    def on_status(self, tweet):
        try:
            longitude = str(tweet.coordinates['coordinates'][0])
            latitude =  str(tweet.coordinates['coordinates'][1])
            print longitude
            print latitude
    
            # I added this to update my google spreadsheet with the coordinates
            self.wks.update_acell(('A' + str(self.n)), longitude)
            self.wks.update_acell(('B' + str(self.n)), latitude)
            print "Spreadsheet updated!"
    
            # This is for my counter
            self.n += 1
    

    这不应该是str(self.n)
    ?谢谢你的建议!我刚试过。还是一样…那不应该是
    str(self.n)
    ?谢谢你的建议!我刚试过。还是一样…非常感谢你的回答!不幸的是,它给了我同样的结果(你能删除状态
    上的
    中的
    尝试
    /
    除外
    行,然后发布发生的情况吗?我假设出现了一个错误,可能会提供一些有用的信息,但是
    除外:传递
    部分正在丢弃这些信息。非常感谢你的回答!不幸的是,它给了我同样的信息。)结果:(你能删除状态
    上的
    中的
    尝试
    /
    除外
    行,然后发布发生了什么事吗?我假设出现了一个错误,可以提供一些有用的信息,但是
    除外:传递
    部分正在丢弃这些信息。哦!好的。我正在编辑我的帖子。对不起!我是Stackoverflow新手!:)哦!好的。我正在编辑我的文章。对了。对不起!我是一个Stackoverflow新手!:)