如何从 python 脚本中 运行 snscrape 命令?
How to run snscrape command from python script?
我正在尝试使用 snscrape 下载一些推文。安装后,我可以 运行 像下面这样的命令来下载一些推文:
snscrape --jsonl --max-results 4 twitter-search "#SherlockHolmes since:2015-01-01 until:2015-01-15" > sherlock_tweets.json
现在我想从 python 脚本中 运行 这个命令。据我了解,这样做的方法是使用 subprocess.run 方法。我使用以下代码 运行 来自 python 的命令:
import subprocess
# Running this in a terminal works
cmd = '''snscrape --jsonl --max-results 4 twitter-search "#SherlockHolmes since:2015-01-01 until:2015-01-15" > sherlock_tweets.json'''
arglist = cmd.split(" ")
process = subprocess.run(arglist, shell=True)
运行 然而,这给出了以下错误。
usage: snscrape [-h] [--version] [-v] [--dump-locals] [--retry N] [-n N] [-f FORMAT | --jsonl] [--with-entity] [--since DATETIME] [--progress]
{telegram-channel,weibo-user,vkontakte-user,instagram-user,instagram-hashtag,instagram-location,twitter-thread,twitter-search,reddit-user,reddit-subreddit,reddit-search,facebook-group,twitter-user,twitter-hashtag,twitter-list-posts,facebook-user,facebook-community,twitter-profile}
...
snscrape: error: the following arguments are required: scraper
为什么这两种情况下的行为不一样?如何从 python 脚本完成 运行ning 命令,获得与在终端中输入命令完全相同的行为?
我不知道您是否找到了解决方案,但我 运行 这个代码对我有用 :
import pandas as pd
import snscrape.modules.twitter as sntwitter
tweet_collection = pd.DataFrame({
'Username':[],
'Date'=[],
'Likes'=[],
'Content'=[]})
for tweet in sntwitter.TwitterSearchScraper(f'since:{date_beg} until:{date_end} from:{twitter_account}').get_items():
tweets_collection = tweets_candidats.append({
"Username":tweet.user.username,
"Date":tweet.date,
"Tweet":tweet.content,
"Likes":tweet.likeCount,},ignore_index=True)
tweets_candidats.to_csv('Path/file.csv')
您可以在 git hub
上的代码中找到更多详细信息
Twitter snscrape arguments
我正在尝试使用 snscrape 下载一些推文。安装后,我可以 运行 像下面这样的命令来下载一些推文:
snscrape --jsonl --max-results 4 twitter-search "#SherlockHolmes since:2015-01-01 until:2015-01-15" > sherlock_tweets.json
现在我想从 python 脚本中 运行 这个命令。据我了解,这样做的方法是使用 subprocess.run 方法。我使用以下代码 运行 来自 python 的命令:
import subprocess
# Running this in a terminal works
cmd = '''snscrape --jsonl --max-results 4 twitter-search "#SherlockHolmes since:2015-01-01 until:2015-01-15" > sherlock_tweets.json'''
arglist = cmd.split(" ")
process = subprocess.run(arglist, shell=True)
运行 然而,这给出了以下错误。
usage: snscrape [-h] [--version] [-v] [--dump-locals] [--retry N] [-n N] [-f FORMAT | --jsonl] [--with-entity] [--since DATETIME] [--progress]
{telegram-channel,weibo-user,vkontakte-user,instagram-user,instagram-hashtag,instagram-location,twitter-thread,twitter-search,reddit-user,reddit-subreddit,reddit-search,facebook-group,twitter-user,twitter-hashtag,twitter-list-posts,facebook-user,facebook-community,twitter-profile}
...
snscrape: error: the following arguments are required: scraper
为什么这两种情况下的行为不一样?如何从 python 脚本完成 运行ning 命令,获得与在终端中输入命令完全相同的行为?
我不知道您是否找到了解决方案,但我 运行 这个代码对我有用 :
import pandas as pd
import snscrape.modules.twitter as sntwitter
tweet_collection = pd.DataFrame({
'Username':[],
'Date'=[],
'Likes'=[],
'Content'=[]})
for tweet in sntwitter.TwitterSearchScraper(f'since:{date_beg} until:{date_end} from:{twitter_account}').get_items():
tweets_collection = tweets_candidats.append({
"Username":tweet.user.username,
"Date":tweet.date,
"Tweet":tweet.content,
"Likes":tweet.likeCount,},ignore_index=True)
tweets_candidats.to_csv('Path/file.csv')
您可以在 git hub
上的代码中找到更多详细信息Twitter snscrape arguments