【问题标题】:MongoDB delete documents not containing custom wordsMongoDB删除不包含自定义词的文档
【发布时间】:2021-06-21 05:39:10
【问题描述】:

我有一个新闻文章抓取工具,可以根据某些内容提取文章。有时,爬虫会拉回与他们应该做的无关的文章。

我想删除包含相关关键字的文档。我在 pandas 中运行了以下代码,并成功删除了不需要的文档:

relevant_words = ['Bitcoin', 'bitcoin', 'Ethereum', 'ethereum', 'Tether', 'tether', 'Cardano', 'cardano', 'XRP', 'xrp']
articles['content'] = articles['content'][articles['content'].str.contains('|'.join(relevant_words))].str.lower()
articles.dropna(subset=['content'], inplace=True)

我的数据库结构如下:

_id:
title:
url:
description:
author:
publishedAt:
content:
source_id:
urlToImage:
summarization:

content 字段可以包含从一个句子到多个段落的任何内容。我正在考虑一个 Python 脚本,它遍历 content 字段,查找没有相关单词的文档并删除它们。

【问题讨论】:

    标签: pandas mongodb aggregation-framework mongodb-atlas


    【解决方案1】:

    过滤到一个新的数据框。 你走在正确的轨道上,除了你必须按以下顺序走;

    加入并转换为小写:

    '|'.join(relevant_words).lower()
    

    过滤器

    m= articles['content'].str.contains('|'.join(relevant_words).lower())
    

    屏蔽过滤器

    articles[m]
    

    组合码

    new_articles=articles[articles['content'].str.contains('|'.join(relevant_words).lower())]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-29
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 2013-09-14
      • 2016-04-20
      • 2010-10-11
      相关资源
      最近更新 更多