使用Snscrape爬取Twitter数据报错:'Tweet'对象无'likeCount'属性
AttributeError: 'Tweet' object has no attribute 'likeCount' 问题解决
问题场景
运行以下snscrape爬取Twitter数据的代码时:
import snscrape.modules.twitter as sntwitter import pandas as pd # Creating list to append tweet data to attributes_container = [] # Using TwitterSearchScraper to scrape data and append tweets to list for i,tweet in enumerate(sntwitter.TwitterSearchScraper('sex for grades since:2021-07-05 until:2022-07-06').get_items()): if i>150: break attributes_container.append([tweet.user.username, tweet.date, tweet.likeCount, tweet.sourceLabel, tweet.content]) # Creating a dataframe to load the list tweets_df = pd.DataFrame(attributes_container, columns=["User", "Date Created", "Number of Likes", "Source of Tweet", "Tweet"])
触发错误:
AttributeError: 'Tweet' object has no attribute 'likeCount'
错误原因
snscrape在v0.5.0及以后的版本迭代中,调整了Tweet对象的属性命名规则:从驼峰式命名改为下划线式命名。旧代码中使用的likeCount(点赞数)、sourceLabel(推文来源)这类驼峰属性名已被移除,替换为对应的下划线命名属性。
修复方案
将代码中的属性名替换为新版本的下划线命名:
tweet.likeCount→tweet.like_counttweet.sourceLabel→tweet.source_label
修复后的完整代码:
import snscrape.modules.twitter as sntwitter import pandas as pd # Creating list to append tweet data to attributes_container = [] # Using TwitterSearchScraper to scrape data and append tweets to list for i,tweet in enumerate(sntwitter.TwitterSearchScraper('sex for grades since:2021-07-05 until:2022-07-06').get_items()): if i>150: break attributes_container.append([tweet.user.username, tweet.date, tweet.like_count, tweet.source_label, tweet.content]) # Creating a dataframe to load the list tweets_df = pd.DataFrame(attributes_container, columns=["User", "Date Created", "Number of Likes", "Source of Tweet", "Tweet"])
内容的提问来源于stack exchange,提问作者hard worker
相关产品推荐
相关产品推荐

