【问题标题】:Twitter API: Pull Tweets by a user AND containing a keyword using Twython (Python)Twitter API:由用户提取推文并使用 Twython (Python) 包含关键字
【发布时间】:2017-02-14 00:16:16
【问题描述】:

我是 Python 和 Twython 的新手,可以使用#3 的一些帮助。我正在做一些研究,并希望通过用户提取推文并包含某个关键字。我也想将它导出到 csv 但弄清楚它是第一部分。谢谢:)

# Bring in the module Twython which pulls from Twitter's API
from twython import Twython, TwythonError

# Making variables for my twitter API keys 
CONSUMER_KEY = 'my personal input hered'
CONSUMER_SECRET = 'my personal input here'
ACCESS_KEY = 'my personal input here'
ACCESS_SECRET = 'my personal input here'

twitter = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)

## 1) This block works and searches for tweets containing a specific keyword
print(twitter.search(q='python'))

## 2) This block works as well and returns all tweets by username
try:
   user_timeline = twitter.get_user_timeline(screen_name='')
except TwythonError as e:
   print e
for tweets in user_timeline:
print tweets['text']

## 3) I can't get this one to work. It is supposed to return all tweets by
#     username AND containing keyword
*try:
   user_timeline = twitter.get_user_timeline(screen_name='ThePSF') and twitter.search(q='lines')
except TwythonError as e:
   print e
for tweets in user_timeline and q:
   print tweets['text']*

【问题讨论】:

    标签: python api twitter twython


    【解决方案1】:

    Twitter API 不允许您在用户时间线内进行搜索 - 您必须在 #2 中自己进行搜索。

    您在 #3 中所做的另一种方法是仅使用搜索 API(在本例中为 twitter.search)并传入涵盖这两种情况的查询。一个例子是

    twitter.search(q='from:ThePSF AND lines')
    

    但请注意,搜索 API 仅限于 7 天的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      相关资源
      最近更新 更多