質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

2回答

2566閲覧

python3で"ImportError: cannot import name 'BeautifulSoup'"が解除できない

kutu

総合スコア257

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2020/03/06 04:48

お世話になっております。下記の環境で

・Windows10-WSL ・Ubuntu18.04 ・python3.6.8

以下のコードを実行いたしました

import requests from bs4 import BeautifulSoup # スクレイピング対象の URL にリクエストを送り HTML を取得する res = requests.get('http://ooooooo.com/') # レスポンスの HTML から BeautifulSoup オブジェクトを作る soup = BeautifulSoup(res.text, 'html.parser') # ページに含まれるリンクを全て取得する links = [url.get('href') for url in soup.find_all('a')] print(links)

結果はこの通りです

Traceback (most recent call last): File "html.py", line 2, in <module> from bs4 import BeautifulSoup File "/home/kutu/.pyenv/versions/3.6.8/lib/python3.6/site-packages/bs4/__init__.py", line 31, in <module> from .builder import builder_registry, ParserRejectedMarkup File "/home/kutu/.pyenv/versions/3.6.8/lib/python3.6/site-packages/bs4/builder/__init__.py", line 7, in <module> from bs4.element import ( File "/home/kutu/.pyenv/versions/3.6.8/lib/python3.6/site-packages/bs4/element.py", line 19, in <module> from bs4.formatter import ( File "/home/kutu/.pyenv/versions/3.6.8/lib/python3.6/site-packages/bs4/formatter.py", line 1, in <module> from bs4.dammit import EntitySubstitution File "/home/kutu/.pyenv/versions/3.6.8/lib/python3.6/site-packages/bs4/dammit.py", line 13, in <module> from html.entities import codepoint2name File "/mnt/c/Users/shinya.kutsukake/public_html/python/html.py", line 2, in <module> from bs4 import BeautifulSoup ImportError: cannot import name 'BeautifulSoup'
pip install beautifulsoup4

は既に実行してあり

Requirement already satisfied: beautifulsoup4 in /home/kutu/.pyenv/versions/3.6.8/lib/python3.6/site-packages (4.8.2) Requirement already satisfied: soupsieve>=1.2 in /home/kutu/.pyenv/versions/3.6.8/lib/python3.6/site-packages (from beautifulsoup4) (2.0)

となり、

pip install beautifulsoup

Collecting beautifulsoup Using cached BeautifulSoup-3.2.2.tar.gz (32 kB) ERROR: Command errored out with exit status 1: command: /home/kutu/.pyenv/versions/3.6.8/bin/python3.6 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-dkg1gbs9/beautifulsoup/setup.py'"'"'; __file__='"'"'/tmp/pip-install-dkg1gbs9/beautifulsoup/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-dkg1gbs9/beautifulsoup/pip-egg-info cwd: /tmp/pip-install-dkg1gbs9/beautifulsoup/ Complete output (6 lines): Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-install-dkg1gbs9/beautifulsoup/setup.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.

となります。
おわかりになる方いらっしゃいましたらよろしくお願いします

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

https://docs.python.org/ja/3/library/html.html
htmlという標準ライブラリがあって、それを使おうとしているのですが、あなたが置いたhtml.pyをロードしてしまって失敗しています。


plain

1 File "/home/kutu/.pyenv/versions/3.6.8/lib/python3.6/site-packages/bs4/dammit.py", line 13, in <module> 2 from html.entities import codepoint2name

の部分、bs4/dammit.pyというファイルで、from html.entities import codepoint2name
をしようとして、次の行が

plain

1 File "/mnt/c/Users/shinya.kutsukake/public_html/python/html.py", line 2, in <module>

ですのでそれと分かります。

投稿2020/03/06 05:29

quickquip

総合スコア11061

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

File "/mnt/c/Users/shinya.kutsukake/public_html/python/html.py", line 2, in <module>

あなたが実行しているpyスクリプトのファイル名は「html.py」という名前ですか?

pythonには「html」というモジュールがありますので、ファイル名を変えてみてください。

>>> import html >>> html <module 'html' from '/Users/ユーザ名/.pyenv/versions/3.7.6/lib/python3.7/html/__init__.py'>

投稿2020/03/06 05:30

technocore

総合スコア7247

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問