【问题标题】:Unable to download twitter data无法下载推特数据
【发布时间】:2017-04-05 18:58:30
【问题描述】:
def get_tweets(api, input_query):
    for tweet in tweepy.Cursor(api.search, q=input_query,lang="en").items():
    yield tweet

if __name__ == "__version__":
    input_query = sys.argv[1]

    access_token = "REPLACE_YOUR_KEY_HERE"
    access_token_secret = "REPLACE_YOUR_KEY_HERE"
    consumer_key = "REPLACE_YOUR_KEY_HERE"
    consumer_secret = "REPLACE_YOUR_KEY_HERE"
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    tweets = get_tweets(api, input_query)
    for tweet in tweets:
        print(tweet.text)

我正在尝试使用命令提示符从 Twitter 下载数据。我已经输入了我的密钥(我只是重新创建了它们),将脚本保存为“print_tweets”,并在命令提示符中输入“python print_tweets.py subject”,但没有任何反应,没有错误消息或任何东西。

我认为问题可能与路径环境有关,但我创建了另一个打印出“hello world”的程序,并且使用命令提示符执行该程序没有问题。

任何人都可以看到我上面的代码有任何明显的错误吗?这对你有用吗? 我什至尝试将“版本”更改为“主要”,但这给了我一条错误消息:

如果 名称 == "版本":

【问题讨论】:

  • 你昨天不是也问过同样的问题吗? stackoverflow.com/questions/43219596/…。我提到你需要包括下划线__ -->"__main__",而不是"main",像这样:if __name__ == "__main__":
  • 我想我快到了,但它不会执行。当我输入“main”时,我收到此错误 NameError Traceback (most recent call last) in () 4 5 if name == "main": ----> 6 input_query = sys.argv[1] 7 8 access_token = "X...." NameError: name 'sys' is not defined跨度>
  • import sys 作为脚本顶部的第一行
  • 如果您在 ipython 解释器中运行这一切,您可以将 input_query = sys.argv[1] 替换为 input_query = "the topic you want to search"

标签: python twitter


【解决方案1】:

您似乎在 ipython 解释器中运行脚本,它不会接收任何命令行参数。试试这个:

import tweepy

def get_tweets(api, input_query):
    for tweet in tweepy.Cursor(api.search, q=input_query,lang="en").items():
        yield tweet

input_query = "springbreak"  # Change this string to the topic you want to search tweets

access_token = "REPLACE_YOUR_KEY_HERE"
access_token_secret = "REPLACE_YOUR_KEY_HERE"
consumer_key = "REPLACE_YOUR_KEY_HERE"
consumer_secret = "REPLACE_YOUR_KEY_HERE"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
tweets = get_tweets(api, input_query)
for tweet in tweets:
    print(tweet.text)

【讨论】:

    猜你喜欢
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2012-05-02
    • 1970-01-01
    • 2020-05-07
    相关资源
    最近更新 更多