标签: tweepy

在Python中解析Twitter JSON对象

我想从twitter上下载推文.

我为此使用了python和Tweepy.虽然我是Python和Twitter API的新手.

我的Python脚本如下:#!usr/bin/python

#import modules
import sys
import tweepy
import json

#global variables
consumer_key = ''
consumer_secret = ''
token_key = ''
token_secret = ''

#Main function
def main():
    print sys.argv[0],'starts'
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(token_key, token_secret)
    print 'Connected to Twitter'
    api = tweepy.API(auth)
    if not api.test():
        print 'Twitter API test failed'

    print 'Experiment with cursor'
    print 'Get search method returns json objects'

   json_search = api.search(q="football")
   #json.loads(json_search())
   print  json_search


#Standard boilerplate to call main function if this file runs …
Run Code Online (Sandbox Code Playgroud)

python twitter json tweepy

1
推荐指数
1
解决办法
1万
查看次数

如何在 tweepy 中刷新 Twitter 过滤器流线程?

我使用 tweepy 来跟踪带有更改主题标签列表的推文

twitterStream=Stream(OAuthObject,listenerFunction())
twitterStream.filter(track=[u'hashtags separated by comma'])
Run Code Online (Sandbox Code Playgroud)

现在每 3 小时,我应该从数据库中获取最新的主题标签,并刷新流线程,我该怎么做?

python twitter multithreading tweepy

1
推荐指数
1
解决办法
2026
查看次数

在tweepy中循环错误

我最近一直在玩弄tweepy,我试图吸引追随者并跟随给定用户.

 followingids = []
 followids = []
 userid = "Someone"#sets target

 for page in tweepy.Cursor(api.followers_ids, screen_name=userid).pages():#gets the followers for userID
     followingids.extend(page)
     time.sleep(60)#keeps us cool with twitter


  for page in tweepy.Cursor(api.friends_ids, screen_name=userid).pages():#gets the followers for userID
     followids.extend(page)
     time.sleep(60)#keeps us cool with twitter

 #where weirdness starts 
 print len(followingids), "followers have been gathered from", userid 
 print len(followids), " users are followed by ", userid

 followingusers = api.lookup_users(user_ids=followingids)#ieterates through the list of users and prints them
 followedusers = api.lookup_users(user_ids=followids) #<does not work but above …
Run Code Online (Sandbox Code Playgroud)

printing for-loop tweepy

1
推荐指数
1
解决办法
834
查看次数

在Tweepy中优雅地处理user_timeline方法的错误和异常

我正在为大量用户收集推文,因此该脚本将在无人监督的情况下运行数天/数周.我有一个user_ids列表big_list.我认为有些推文是私有的,我的脚本会停止,所以我想让脚本继续使用下一个user_id(并且可能会打印一条警告消息).

我还想了解如何使其对其他错误或异常具有鲁棒性(例如,脚本在出错或超时时休眠)

这是我的总结:

import tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
my_api = tweepy.API(auth)

for id_str in big_list:
    all_tweets = get_all_tweets(id_str=id_str, api=my_api)
    #Here: insert some tweets into my database
Run Code Online (Sandbox Code Playgroud)

get_all_tweets函数抛出错误,它基本上反复调用:

my_api.user_timeline(user_id = id_str, count=200)
Run Code Online (Sandbox Code Playgroud)

以防万一,它给出的回溯如下:

/home/username/anaconda/lib/python2.7/site-packages/tweepy/binder.pyc in execute(self)
    201                 except Exception:
    202                     error_msg = "Twitter error response: status code = %s" % resp.status
--> 203                 raise TweepError(error_msg, resp)
    204 
    205             # Parse the response payload

TweepError: Not authorized.
Run Code Online (Sandbox Code Playgroud)

如果您需要更多详细信息,请告诉我们.谢谢!

-----------编辑--------

这个问题有一些信息.

我想我可以尝试try/except为不同类型的错误做一个块?我不知道所有相关的,所以有现场经验的人的最佳实践将不胜感激!

----------编辑2 …

python twitter tweepy

1
推荐指数
2
解决办法
7686
查看次数

较旧的推文 Tweepy 使用 Python

我正在尝试使用 Python 中的 tweepy 获取较旧的推文数据(大约 2 个月大)。我试过自和直到参数但没有成功。有没有人在 tweepy 或其他一些 API 中得到解决。

for id,tweet in enumerate (tweepy.Cursor(api.search, q='SpecificWord', since="2016-04-26", until="2016-04-28", lang="en", include_retweets=False    ).items(200)):
     #Write a row to the csv file
     CSVW.writerow([tweet.created_at,    tweet.retweet_count,    tweet.text.encode('utf-8')])
Run Code Online (Sandbox Code Playgroud)

python twitter tweepy

1
推荐指数
1
解决办法
4483
查看次数

使用 tweepy 获取独特的推文

我正在尝试使用许多搜索词来获取推文语料库。我遇到的一个问题是它无法获得独特的推文。也就是转发。

有没有办法在不进行任何文本处理的情况下预先删除这些?

我现在有什么:

 api=tweepy.API(auth)
 for search in hashtags:
     for tweet in  tweepy.Cursor(api.search,q=search,count=1000,lang="en").items(): 
         text=repr(tweet.text.encode("utf-8"))  
         out.write(text+"\n")
Run Code Online (Sandbox Code Playgroud)

python twitter tweepy

1
推荐指数
1
解决办法
1175
查看次数

Tweepy 使用用户名查找 ID

我正在尝试从我拥有的用户名列表中获取 ID 列表。tweepy 是否提供了任何方法可以让我使用用户名查找用户 ID?

python twitter tweepy

1
推荐指数
1
解决办法
2836
查看次数

绕过 IncompleteRead 异常

我正在使用 Tweepy 在 Python3 中编写 Twitter 流侦听器。流一段时间后我收到此错误:

urllib3.exceptions.ProtocolError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))
Run Code Online (Sandbox Code Playgroud)

我怎么能绕过这个,重新连接并继续?

我已经做好了:

from requests.packages.urllib3.exceptions import ReadTimeoutError, IncompleteRead
Run Code Online (Sandbox Code Playgroud)

和:

while True:
    try:
        twitter_stream.filter(track=keywordlist, follow=userlist)

    except IncompleteRead:
        continue
Run Code Online (Sandbox Code Playgroud)

但仍然收到错误。

python urllib3 tweepy

1
推荐指数
1
解决办法
2138
查看次数

如何使用 JSON 从 twitter API 获取完整的状态文本?

tldr:如何使用 JSON 访问完整的推文正文?

你好

我在 JSON 中查找推文全文时遇到问题。

我正在用 tweepy 制作一个 python 应用程序。我想获取状态,然后访问文本

编辑

我曾经user_timeline()得到一个tweet_list. 然后从他们那里收到一条推文,如下所示:

tweet=tweet_list[index]._json
Run Code Online (Sandbox Code Playgroud)

现在当我这样做时:

tweet['text']
Run Code Online (Sandbox Code Playgroud)

它返回一条缩短的推文,并带有原始推文的链接

例如:

美国黑人的失业率是有史以来最低的。特朗普对美国黑人的支持率翻了一番......(缩短的链接,由于stackoverflow规则无法直接链接)

我想返回这个:

美国黑人的失业率是有史以来最低的。特朗普对美国黑人的支持率翻了一番。谢谢你,它会变得更好!@福克斯新闻

只要显示完整的推文,我不介意是否添加链接

python api twitter json tweepy

1
推荐指数
1
解决办法
2824
查看次数

用python读取json字符串的特定部分

我目前正在研究django环境中的一个程序,该程序使用第三方提供的json api.我想要的API中有一个对象,但是它提供的信息对我来说太过分了.

我想要的数据是使用tweepy来自twitter api的created_at标签.此created_at包含以下格式的数据:

"created_at": "Mon Aug 27 17:21:03 +0000 2012"
Run Code Online (Sandbox Code Playgroud)

这一切都很好但是这将返回日期和时间,而我只是想要上面例子的时间部分,即17:21:03.

有什么办法可以把created_at响应字符串的这一部分存储起来并存储在一个单独的变量中吗?

python django twitter json tweepy

1
推荐指数
1
解决办法
175
查看次数

标签 统计

tweepy ×10

python ×9

twitter ×8

json ×3

api ×1

django ×1

for-loop ×1

multithreading ×1

printing ×1

urllib3 ×1