【问题标题】:Error on getting user id from twitter using tweepy?使用 tweepy 从 Twitter 获取用户 ID 时出错?
【发布时间】:2018-04-24 12:39:54
【问题描述】:

我的代码

#!/usr/bin/python
import tweepy
import csv #Import csv
auth = tweepy.auth.OAuthHandler('XXXXXX', 'XXXXXXX')
auth.set_access_token('XXX-XXX', 'XXX')

api = tweepy.API(auth)

# Open/create a file to append data to
csvFile = open('result.csv', 'a')

#Use csv writer
csvWriter = csv.writer(csvFile)

for tweet in tweepy.Cursor(api.search,
                       q = "google",
                       since = "2014-02-14",
                       until = "2014-02-15",
                       lang = "en").items():

# Write a row to the CSV file. I use encode UTF-8
csvWriter.writerow([tweet.created_at,tweet.user, tweet.text.encode('utf-8')])
print tweet.created_at, tweet.text
csvFile.close()

在这里我无法在输出中获取 user_id。 输出是

2018-04-24 10:54:34 RT @BoldContentTV:很高兴为@StylistMagazine 和 .以才华横溢的@ericafmstyle 为特色,#Fashi…

是什么原因?以及如何获取用户 ID?我想要三个方面的日期/用户名/文本。

我得到了代码from

【问题讨论】:

    标签: python python-2.7 twitter tweepy


    【解决方案1】:

    你可以得到,

    用户名:tweet.user.screen_name

    文本为:tweet.text

    created_at : tweet.created_at

    示例代码:

    In [33]: import tweepy
    
    In [34]: auth = tweepy.OAuthHandler('', '')
    
    In [35]: auth.set_access_token('', '')
    
    In [36]: api = tweepy.API(auth)
    
    In [37]: for tweet in tweepy.Cursor(api.search, q = "nishant").items(5):
        ...:     print tweet.user.screen_name , tweet.text, tweet.created_at
        ...:
    
    GuruInAJeans RT @periferrydotcom: Shaina's story is all about courage and the will to achieve.
    it shows how patience can go a long way.
    
    Full Story: htt… 2018-04-24 13:02:42
    bajjiladeva @ts_nishant @KTRTRS neku ikkada first nene chepta bro inauguration date .. 2018-04-24 12:57:37
    ts_nishant Don’t @KTRTRS <twitter short url> 2018-04-24 12:55:43
    ts_nishant When @KTRTRS ?? <twitter short url> 2018-04-24 12:55:13
    parivesh100 Nishant Shah on the invisible environmental costs that the digital age has brought - <twitter short url> 2018-04-24 12:47:56
    

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 2021-02-27
      • 2020-06-05
      • 2018-03-07
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多