【问题标题】:Cannot install BeautifulSoup for python 3.6无法为 python 3.6 安装 BeautifulSoup
【发布时间】:2017-10-05 17:01:43
【问题描述】:

在过去的 45 分钟里,我一直在绝望地尝试跑步:

from bs4 import BeautifulSoup

但无济于事。我已经尝试了命令:

python -m pip install beautifulsoup4

上面写着:

Requirement already satisfied: beautifulsoup4 in c:\python27\lib\site-packages

我试过了:

pip3 install beautifulsoup4

它说的一样。

我试过了:

pip install beautifulsoup4

同样的事情。

我已经查看了整个 stackoverflow,youtube,我正在疯狂地试图弄清楚这一点。我不知道该怎么办,请帮帮我。

当我尝试使用以下代码运行我的程序 main.py 时:

from bs4 import BeautifulSoup

使用 py -3 main.py,我得到错误:

ModuleNotFoundError: No module named 'bs4'

请帮帮我。

我尝试了BeautifulSoup4 can't be installed in python3.5 on Windows7提出的方法,但无济于事。

【问题讨论】:

  • 你有另一个版本的 Python 3,这就是 pip3 所指向的。你如何使用你的口译员? py -3 ?
  • 您使用的是哪个解释器?您安装了两个版本的Python。也许在外壳上使用python3
  • 当我执行“python3 main.py”时,我得到:'python3' 不是内部或外部命令、可运行程序或批处理文件。
  • 我想你在 bs4 中安装了 BeautifulSoup

标签: python beautifulsoup


【解决方案1】:

只需安装:

pip3 install bs4 --user

在代码中导入:

from bs4 import BeautifulSoup
import requests

res = requests.get(url)
soup = BeautifulSoup(res.content,"html.parser")

【讨论】:

    【解决方案2】:

    感谢 noobninja,

    至于将 Python 3.8 扩展到 BeautifulSoup4,仅使用 ANACONDA 对我有效(使用 MAC OS)。

    1. 下载适用于 Mac OS 的 Anaconda(超过 2 GB)
    2. 在终端:测试 Anaconda 安装:

      $ conda 列表

    3. $ conda install beautifulsoup4
    4. 运行 Python3 外壳
    5. 类型:从 bs4 导入 BeautifulSoup

    没有错误信息!

    【讨论】:

      【解决方案3】:

      我在 Mac 上遇到了同样的错误,而当我认为我使用的是 3.7 时,问题源于使用 Python 2.7.10。

      python test.py
      
      File "test.py", line 1, in <module>
          from bs4 import BeautifulSoup
      ImportError: No module named bs4
      

      但是,当我明确说明我想使用哪个 python 版本时,一切正常。所以在终端上使用 python 然后你的版本号。例如我的版本是 3.7:

      python3.7 test.py
      

      【讨论】:

        【解决方案4】:

        我经历了类似的事情,但设法安装了 BeautifulSoup4。我尝试运行建议的命令,但都没有奏效。所以这就是我所做的。

        在命令提示符下,我决定尝试在 python 脚本目录中运行命令

        cd C:\Users\User\AppData\Local\Programs\Python\Python36-32\Scripts
        

        然后

        pip3 install BeautifulSoup4
        

        你应该会看到一些内容

        Collecting BeautifulSoup4
         Downloading beautifulsoup4-4.6.0-py3-none-any.whl (86kB)
           100% |████████████████████████████████| 92kB 6.7kB/s
        Installing collected packages: BeautifulSoup4
        Successfully installed BeautifulSoup4-4.6.0
        

        希望这会有所帮助。

        【讨论】:

          【解决方案5】:

          现在有适用于 Python 3.6 的 beautifulsoup4。 也一样,我的webcrawl项目就是这样用的。

          只需将 beautifulsoup4 模块添加到项目中即可。那就试试线

          from bs4 import BeautifulSoup
          

          【讨论】:

            【解决方案6】:

            改为安装:https://anaconda.org/(这是我们在工作中使用的)

            它基本上是安装了前 100 个模块的 Python。缺点是,它很大(300 MB)。

            【讨论】:

            • 能够使用 Anaconda 为 Python 3.6 安装 BeautifulSoup 4.6.0:conda install beautifulsoup4。 Anaconda 的 pkg 参考:beautifulsoup4: 4.6.0-py36_0。具体 Python 版本:3.6.2。 注意:我将 BS4 安装到 python 虚拟环境中。
            【解决方案7】:

            到目前为止,该模块还没有加载到 python3.6 试试这个

            python3.6 -m pip install beautifulsoup4
            

            【讨论】:

            • 这个应该可以的。 Sohaibs 的评论没用,我有 py 3.6.1 它可以工作。如果这不起作用,我将发布一个有利弊的解决方案。
            • Simple python -m pip ... 在 3.7 版中为我工作。
            • init.py 文件中有一条消息:'You are trying to run the Python 2 version of Beautiful Soup under Python 3. This will not work.'!='You need to convert the code, either by installing it (python setup.py install) or by running 2to3 (2to3 -w bs4).'
            【解决方案8】:

            问题在于,虽然它安装了 beautifulsoup4,但目前 Python 3.6 不支持几个库。

            你有两个选择。您可以按照 Alan 的建议使用像 Anaconda 或 virtualenv 这样的虚拟环境。在这里你将创建一个环境并将python版本设置为3.5

            另一种选择是卸载 python 3.6 并安装 3.5,然后是 beautifulsoup4

            【讨论】:

              猜你喜欢
              • 2017-11-23
              • 1970-01-01
              • 2018-07-06
              • 1970-01-01
              • 1970-01-01
              • 2019-06-19
              • 1970-01-01
              • 2017-07-11
              • 1970-01-01
              相关资源
              最近更新 更多