遵循代码
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‘对象没有属性'likeCount’。
有人知道为什么会这样吗?
谢谢。
发布于 2022-09-28 21:54:52
您需要安装开发版本的snscrape才能获得像likeCount
这样的属性。您可以尝试安装开发人员版本并再次运行代码;或者,如果您只对当前版本提供的内容满意,则可以尝试:
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(
dict(
(name, getattr(tweet, name)) for name in dir(tweet)
if not name.startswith('_') and name not in ['count','index']
)
)
tweets_df = pd.DataFrame(attributes_container)
然后tweets_df.columns
给出了输出:
Index(['content', 'date', 'id', 'outlinks', 'outlinksss', 'tcooutlinks',
'tcooutlinksss', 'url', 'username'],
dtype='object')
这些数据是我唯一能用我的版本的snscrape访问的数据。你可能会得到不同的东西。
https://stackoverflow.com/questions/73874328
复制相似问题