首页 > 解决方案 > VS Code 中的代码运行程序无法将 Beautiful Soup 识别为模块

问题描述

我正在做一个基本的文档摘要,我正在使用 BeautifulSoup 来解析 VS Code 中的文档。

当我在终端中运行我的代码时,它运行时没有错误。但是,当我使用 Code Runner 获取输出时,它返回错误“ImportError: No module named bs4”。我认为这可能是 bs4 的问题,所以我尝试了一些导入格式的变体:

from bs4 import BeautifulSoup4 as bs
import BeautifulSoup as bs
import BeautifulSoup4 as bs

我用 pip 将 bs4 卸载并重新安装到 Python 2.7 和 Python3.7。

这是我认为应该工作的代码的上下文。

import urllib.request
import re
scraped_data=urllib.request.urlopen('http://test.com/path.pdf')
article = scraped_data.read()

parsed_Article=bs.BeautifulSoup(article, 'lxml')

它应该将 bs4 识别为 BeautifulSoup4 并运行它来解析网页文档。相反,它只是给出不存在名为 bs4 的模块的错误。所有代码的最终结果将是 url 文档的 7 句摘要。我相信这是一个 Code Runner 问题,因为它在终端中运行良好。因为我需要 print(summary) 作为输出,所以我使用的是 Code Runner。也许我应该使用不同的程序来运行代码并获取 7 句摘要。

标签: pythonbeautifulsoupvscode-code-runner

解决方案


推荐阅读