[Python] 파이썬 BeautifulSoup 설치 오류시 해결 방법 : os.system(), pip install beautifulsoup4
import 문에 빨간줄이 표시가되어 마우스를 올렸다.
BeautifulSoup 모듈을 사용하기위해서는 패키지를 설치해야한다. 클릭하여 자동설치를 시작하였으나 바로 오류가 터졌다.
Collecting BeautifulSoup
Downloading BeautifulSoup-3.2.2.tar.gz (32 kB)
DEPRECATION: The -b/--build/--build-dir/--build-directory option is deprecated. pip 20.3 will remove support for this functionality. A possible replacement is use the TMPDIR/TEMP/TMP environment variable, possibly combined with --no-clean. You can find discussion regarding this at https://github.com/pypa/pip/issues/8333.
ERROR: Command errored out with exit status 1:
command: 'C:UsersilikeAppDataLocalProgramsPythonPython39python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:UsersilikeAppDataLocalTemppycharm-packagingbeautifulsoupsetup.py'"'"'; __file__='"'"'C:UsersilikeAppDataLocalTemppycharm-packagingbeautifulsoupsetup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:UsersilikeAppDataLocalTemppip-pip-egg-info-72zafi1p'
cwd: C:UsersilikeAppDataLocalTemppycharm-packagingbeautifulsoup
Complete output (6 lines):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:UsersilikeAppDataLocalTemppycharm-packagingbeautifulsoupsetup.py", line 3
"You're trying to run a very old release of Beautiful Soup under Python 3. This will not work."<>"Please use Beautiful Soup 4, available through the pip package 'beautifulsoup4'."
^
SyntaxError: invalid syntax
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
WARNING: You are using pip version 20.2.3; however, version 20.3.1 is available.
You should consider upgrading via the 'C:UsersilikeAppDataLocalProgramsPythonPython39python.exe -m pip install --upgrade pip' command.
[해결방법] 명령프롬프트(cmd) 창을 열고 pip 업그레이드 명령을 쳐서 업그레이드를 먼저 진행했다.
python.exe -m pip install --upgrade pip
Microsoft Windows [Version 10.0.19042.685]
(c) 2020 Microsoft Corporation. All rights reserved.
C:Usersilike>python.exe -m pip install --upgrade pip
Collecting pip
Downloading pip-20.3.1-py2.py3-none-any.whl (1.5 MB)
|████████████████████████████████| 1.5 MB 6.4 MB/s
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 20.2.3
Uninstalling pip-20.2.3:
Successfully uninstalled pip-20.2.3
Successfully installed pip-20.3.1
C:Usersilike>
하지만 안타깝게도 파이썬3에서는 BeautifulSoup-3.2.2.버전을 사용할 수 없다. BeautifulSoup 4를 사용해야한다.
설치방법은 터미널창을 열거나 명령프롬프트를 열고 pip install beautifulsoup4 명령어를 실행하면 된다.
C:pythonWorkspace>
C:pythonWorkspace>pip install beautifulsoup4
Collecting beautifulsoup4
Downloading beautifulsoup4-4.9.3-py3-none-any.whl (115 kB)
|████████████████████████████████| 115 kB 6.8 MB/s
Collecting soupsieve>1.2
Downloading soupsieve-2.0.1-py3-none-any.whl (32 kB)
Installing collected packages: soupsieve, beautifulsoup4
Successfully installed beautifulsoup4-4.9.3 soupsieve-2.0.1
C:pythonWorkspace>
또다른 설치 방법은 os모듈을 import 후 os.system()함수를 사용하여 명령어를 실행할 수 있다.
>>> import os
>>>
>>> os.system("pip install beautifulsoup4")
Requirement already satisfied: beautifulsoup4 in c:usersilikeappdatalocalprogramspythonpython39libsite-packages (4.9.3)
Requirement already satisfied: soupsieve>1.2 in c:usersilikeappdatalocalprogramspythonpython39libsite-packages (from beautifulsoup4) (2.0.1)
>>>
>>>
마지막으로 import 해야하는 모듈이 변경되었다.
AS-IS
import requests, BeautifulSoup
TO-BE
from bs4 import BeautifulSoup
[REFERENCE]
stackoverflow.com/questions/19957194/install-beautiful-soup-using-pip
[Python BeautifulSoup tutorial]
zetcode.com/python/beautifulsoup/