python - 从网页中仅提取有意义的文本

标签 python web-scraping nlp nltk

我正在获取 url 列表并使用 nltk 抓取它们。我的最终结果是列表的形式,其中包含网页上的所有单词。问题是我只是在寻找不是常见英语“糖”词的关键字和短语,例如“as, and, like, to, am, for”等。我知道我可以构建一个包含所有常见的文件英文单词并简单地将它们从我的抓取标记列表中删除,但是是否有一些库的内置功能可以自动执行此操作?

我本质上是在页面上寻找有用的词,这些词不是乱七八糟的,并且可以为页面的内容提供一些上下文。几乎就像 stackoverflow 上的标签或 google 用于 seo 的标签。

最佳答案

我认为您正在寻找的是 nltk.corpus 中的停用词.words:

>>> from nltk.corpus import stopwords
>>> sw = set(stopwords.words('english'))
>>> sentence = "a long sentence that contains a for instance"
>>> [w for w in sentence.split() if w not in sw]
['long', 'sentence', 'contains', 'instance']

编辑:搜索停用词会给出可能的重复项:Stopword removal with NLTK , How to remove stop words using nltk or python .查看这些问题的答案。并考虑 Effects of Stemming on the term frequency?也是

关于python - 从网页中仅提取有意义的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22848846/

相关文章:

python - 如何使用 python 复制字符串中的字符(就地)?

python - django.db.utils 中的 IntegrityError #1062 - 键 2 的条目重复

r - 从 URL 下载所有 PDF

python - spaCy 2.0 : Save and Load a Custom NER model

python - 使用 PerceptronTagger 阅读我自己的 NLTK 词性标记数据集

python - 将命令输出保存到文件并在终端上查看

python - 在 matplotlib 轴/图上绘图 : get yticklabel individual position and use it for drawlines

python - 带有 mechanicalsoup 的表单请求未显示预期结果

r - 将 tableauViz 抓取到 R 数据帧中

php - 计算各种语言单词的 PHP 库/类?