从(相对)父文件夹导入BeautifulSoup,并在其旁边导入html解析器

问题描述 投票:2回答:1

这是我的文件夹树:

script/
    main.py
    dependencies/
        bs4/
            ...
        requests/
            ...

以下是我从main.py导入模块的方法:

import dependencies.requests as requests
import dependencies.bs4 as bs4

该脚本似乎工作正常,但我无法弄清楚如何将qtml解析器添加到dependencies\与BeautifulSoup一起工作。所以目前在main.py我发出以下GET请求:

response = requests.get(url)

然后尝试解析它:

parsed_html = bs4.BeautifulSoup(response.content, "lxml")

然后我得到以下异常:

File "C:\Users\usr\Desktop\script\dependencies\bs4\__init__.py", line 165, in __init__
% ",".join(features))

dependencies.bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

任何帮助将非常感激。

此外,默认的"html.parser"抛出相同的异常。

python python-3.x beautifulsoup html-parsing lxml
1个回答
1
投票

您应该将PYTHONPATH扩展到安装lxml或html.parser的文件夹。有了这个,“python”将知道在哪个特定位置搜索您的包。

在任何一种情况下,您都在创建具有依赖关系的文件夹结构,因为有一些工具,例如virtualenv

© www.soinside.com 2019 - 2024. All rights reserved.