bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml.

    크롤링을 위해서, BeautifulSoup를 사용하여 작업을 하려는 중, 아래와 같은 에러 발생

     

    bs4.FeatureNotFound, lxml


    에러 메시지

    D:\Study\python\venv\Scripts\python.exe D:/Study/python/coupang.py
    Traceback (most recent call last):
      File "D:/Study/python/coupang.py", line 10, in <module>
        soup = BeautifulSoup(res.text, "lxml")
      File "D:\Study\python\venv\lib\site-packages\bs4\__init__.py", line 245, in __init__
        raise FeatureNotFound(
    bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
    
    Process finished with exit code 1

     

    Import 부분

    import requests
    from bs4 import BeautifulSoup
    import re
    
    ...
    
    
    headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"}
    res = requests.get(URL, headers=headers)
    res.raise_for_status()
    soup = BeautifulSoup(res.text, "lxml")

     

    크롤링을 진행하면서, requests와 bs4를 pip install로 설치하였고, BeautifulSoup의 생성 인자값에 "lxml"을 주었는데 위와 같이 에러가 발생하여 에러 메세지를 확인해보니, lxml을 별도로 설치해야 하는 것으로 보인다.

     

    lxml에 대한 설명

     

    lxml은 Python 언어로 XML 및 HTML을 처리하기 위한 가장 기능이 풍부하고 사용하기 쉬운 라이브러리입니다.[0]

     

    The lxml XML toolkit is a Pythonic binding for the C libraries libxml2 and libxslt.
    It is unique in that it combines the speed and XML feature completeness of these libraries with the simplicity of a native Python API, mostly compatible but superior to the well-known ElementTree API. The latest release works with all CPython versions from 2.7 to 3.9. See the introduction for more information about background and goals of the lxml project. Some common questions are answered in the FAQ.

     

    lxml에 대한 설명

     

    결국 XML과 HTML 구조를 이해하기 위해서 사용되는 "lxml" BeautifulSoup 인자값은 pip로 별도로 설치해야 되는 라이브러리이다.

     

    해결책

    pip install lxml
    > pip install lxml
    Collecting lxml
      Downloading lxml-4.8.0-cp38-cp38-win_amd64.whl (3.6 MB)
         |████████████████████████████████| 3.6 MB ...
    Installing collected packages: lxml
    Successfully installed lxml-4.8.0
    WARNING: You are using pip version 21.3.1; however, version 22.0.4 is available.
    You should consider upgrading via the 'd:\study\python\venv\scripts\python.exe -m pip install --upgrade pip' command.

     

    참고자료

    [0] https://lxml.de/

    반응형

    댓글

    Designed by JB FACTORY