如何下载推特提要 [英] How to download twitter feed

查看:32
本文介绍了如何下载推特提要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编码方面,我是新手.如何修改此示例代码以使用 Python 下载推文?

I am quite the novice when it comes to coding. How do I modify this sample code to download tweets using Python?

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)

我输入了我的密钥.

我应该能够使用类似 print_tweets.py在此处输入主题"这样的命令下载推文我在哪里输入这个命令?在 Python 中?在命令提示符下?

I should be able to download tweets using a command like this print_tweets.py "Enter subject here" Where do I enter this command? In Python? In the command prompt?

我收到此错误:

NameError Traceback(最近调用最后) 在 ()----> 1个print_tweets.py

NameError Traceback (most recent call last) in () ----> 1 print_tweets.py

NameError: name 'print_tweets' 未定义

NameError: name 'print_tweets' is not defined

请帮忙!

推荐答案

NameError 表示 python 脚本没有收到 命令行参数sys.argv[1] 是主题".将在此处输入主题"替换为您要搜索的主题.

The NameError is saying the python script isn't receiving command line arguments, sys.argv[1] being the "subject". Replace "Enter subject here" with the subject you wish to search.

在这个例子中,springbreaksys.argv[1]:

In this example, springbreak is sys.argv[1]:

C:\> python print_tweets.py springbreak

应该返回并打印出包含您的主题"的推文文本.

should return and print out tweet texts containing your "subject".

您可能还需要更改:

if __name__ == "__version__":

if __name__ == "__main__":

as __main__ 是脚本的入口点.

as __main__ is the entry-point to the script.

整个脚本:

#!/usr/bin/env python

import sys
import tweepy

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

if __name__ == "__main__":
    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)

这篇关于如何下载推特提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆