python BeautifulSoup import エラー
mac環境にてpythonを使いウェブスクレイピングを利用したいです。
下記のソースコードにて実行した場合エラーが発生してしまいます。
ファイル名「test.py」
python
1import requests 2from bs4 import BeautifulSoup 3 4url = "https://www.sejuku.net/blog/" 5 6response = requests.get(url) 7response.encoding = response.apparent_encoding 8 9bs = BeautifulSoup(response.text, 'html.parser') 10 11for i in bs.select("h3"): 12 print(i.getText())
python test.py Traceback (most recent call last): File "test.py", line 2, in <module> from bs4 import BeautifulSoup File "/Users/name/.pyenv/versions/3.7.1/lib/python3.7/site-packages/bs4/__init__.py", line 34, in <module> from .builder import builder_registry, ParserRejectedMarkup File "/Users/name/.pyenv/versions/3.7.1/lib/python3.7/site-packages/bs4/builder/__init__.py", line 7, in <module> from bs4.element import ( File "/Users/name/.pyenv/versions/3.7.1/lib/python3.7/site-packages/bs4/element.py", line 19, in <module> from bs4.dammit import EntitySubstitution File "/Users/name/.pyenv/versions/3.7.1/lib/python3.7/site-packages/bs4/dammit.py", line 13, in <module> from html.entities import codepoint2name File "/Users/name/Documents/selenium/html.py", line 2, in <module> from bs4 import BeautifulSoup ImportError: cannot import name 'BeautifulSoup' from 'bs4' (/Users/name/.pyenv/versions/3.7.1/lib/python3.7/site-packages/bs4/__init__.py)
しかし対話モードから実行すると成功します。
pip list Package Version -------------- -------- beautifulsoup4 4.7.1 bs4 0.0.1 certifi 2019.3.9 chardet 3.0.4 cssselect 1.0.3 html5lib 1.0.1 idna 2.8 lxml 4.3.3 pip 19.1.1 requests 2.22.0 setuptools 39.0.1 six 1.12.0 soupsieve 1.9.1 urllib3 1.25.3 webencodings 0.5.1 python -V Python 3.7.1 python Python 3.7.1 (default, May 6 2019, 17:53:22) [Clang 10.0.0 (clang-1000.10.44.4)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 該当プログラムをペーストすると成功する。
同じpythonを使用しているはずがなぜ実行方法が違うとエラーが発生してしまうのでしょうか。
御教授よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー