【问题标题】:Python: How to iterate through a list of Twitter users using Snscrape?Python:如何使用 Snscrap 遍历 Twitter 用户列表?
【发布时间】:2021-06-11 15:41:18
【问题描述】:

我试图通过用户列表检索推文,但是在 snscrape 函数中,此参数在引号内,这使得用户名被视为固定输入

import snscrape.modules.twitter as sntwitter
tweets_list1 = []
users_name = [{'username':'@bbcmundo'},{'username':'@nytimes'}]

for i,tweet in enumerate(sntwitter.TwitterSearchScraper('from:{}').get_items().format(username)):
if i>100:
    break
tweets_list1.append([tweet.date, tweet.id, tweet.content, tweet.url,\
                     tweet.user.username, tweet.user.followersCount,tweet.replyCount,\
                    tweet.retweetCount, tweet.likeCount, tweet.quoteCount, tweet.lang,\
                    tweet.outlinks, tweet.media, tweet.retweetedTweet, tweet.quotedTweet,\
                    tweet.inReplyToTweetId, tweet.inReplyToUser, tweet.mentionedUsers,\
                     tweet.coordinates, tweet.place, tweet.hashtags, tweet.cashtags])

作为输出 Python 获取:

`AttributeError: 'generator' object has no attribute 'format'

此代码可以很好地使用用户名替换花括号并删除 .format 属性。如果您想复制此代码,请务必使用以下命令安装 snscrape 库:

pip install git+https://github.com/JustAnotherArchivist/snscrape.git

非常感谢您能给我的任何指导。

【问题讨论】:

    标签: python twitter scrapy web-scraping-language


    【解决方案1】:

    我发现了我在编写这段代码时所犯的一些错误。因此,我想与大家分享,以防万一您需要它并克服您遇到同样或类似问题的困扰:

    首先:我将 users_name 格式从字典更改为列表项。

    第二:我把格式属性放在了正确的位置。紧跟文本输入功能

    第三:我添加了一个嵌套循环来抓取每个 Twitter 用户帐户

    users_name = ['bbcmundo','nytimes']
    for n, k in enumerate(users_name):
        for i,tweet in enumerate(sntwitter.TwitterSearchScraper('from:{}'.format(users_name[n])).get_items()):
        if i>100:
            break
        tweets_list1.append([tweet.date, tweet.id, tweet.content, tweet.url,\
                             tweet.user.username, tweet.user.followersCount,tweet.replyCount,\
                            tweet.retweetCount, tweet.likeCount, tweet.quoteCount, tweet.lang,\
                            tweet.outlinks, tweet.media, tweet.retweetedTweet, tweet.quotedTweet,\
                            tweet.inReplyToTweetId, tweet.inReplyToUser, tweet.mentionedUsers,\
                             tweet.coordinates, tweet.place, tweet.hashtags, tweet.cashtags])
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 2018-05-22
      相关资源
      最近更新 更多