如何在Python中使用PRAW在Reddit中的所有子Reddit中搜索多个关键字

4zcjmb1e  于 2023-05-05  发布在  Python
关注(0)|答案(1)|浏览(168)

我试图在Reddit的所有subreddits中搜索Reddit帖子中的多个关键词列表。目前,我的数据框只显示关键字_list列表中最后一个单词的结果,即“maritime history”。有谁知道我怎么才能找到所有的单词?

# Creating keyword list
keyword_list = ['maritime heritage', 'shipwreck', 'shipwrecks', 'coastal archeaology', 'maritime archaeology', 
                'maritime ecologies', 'marine archeaology', 'marine heritage', 'cultural marine heritage', 'marine culutral haritage', 'maritime history']

# Searching in all subreddits  
all = reddit.subreddit("all")
df = pd.DataFrame() # creating dataframe for displaying scraped data

# creating lists for storing scraped data
titles=[]
scores=[]
ids=[]

# looping over posts and scraping it
for submission in all.search(keyword_list, limit=None):
    titles.append(submission.title)
    scores.append(submission.score) #upvotes
    ids.append(submission.id)

df['Title'] = titles
df['Id'] = ids
df['Upvotes'] = scores #upvotes

print(df.shape)
df.head(10)
8dtrkrch

8dtrkrch1#

相当直接的改变。你想搜索多个东西,但你不想进行多个搜索(查询)。
你的关键字应该是这样的:

# Creating keyword list
keyword_list = 'maritime heritage, shipwreck, shipwrecks, coastal archeaology, maritime archaeology, maritime ecologies, marine archeaology, marine heritage, cultural marine heritage, marine culutral haritage, maritime history'

这将传递您的单个查询,包含多个关键字,并返回您想要处理的帖子。
只要您当前的后期处理对于'海事历史'响应是正确的,那么您应该没有任何问题处理对新查询的响应。

相关问题