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

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

新規登録して質問してみよう
ただいま回答率
85.50%
スクレイピング

スクレイピングとは、公開されているWebサイトからページ内の情報を抽出する技術です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

1726閲覧

pythonでスクレイピングする際、import urllib.requestが実行できません。

ShunsukeNakao

総合スコア13

スクレイピング

スクレイピングとは、公開されているWebサイトからページ内の情報を抽出する技術です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/10/19 21:05

編集2018/10/20 00:20

pythonを使ってスクレイピングをやりたいのですがbeautifulsoup4はインストールできたのですが
import urllib.request
をやろうとすると
ImportError: No module named request
が出てきてしまって実行できません。
何が原因なのでしょうか?

また、https://github.com/calthoff/self_taught/blob/master/python_ex293.py/
ここにあるものをそのままコピペで対応しようとしたのですが

import urllib.request

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named request

from bs4 import BeautifulSoup

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named bs4

class Scraper:

... def init(self, site):
... self.site = site
...

def scrape(self):

File "<stdin>", line 1
def scrape(self):
^
IndentationError: unexpected indent

r = urllib.request\

File "<stdin>", line 1
r = urllib.request
^
IndentationError: unexpected indent

.urlopen(self.site)

File "<stdin>", line 1
.urlopen(self.site)
^
IndentationError: unexpected indent

html = r.read()

File "<stdin>", line 1
html = r.read()
^
IndentationError: unexpected indent

parser = "html.parser"

File "<stdin>", line 1
parser = "html.parser"
^
IndentationError: unexpected indent

sp = BeautifulSoup(html,

File "<stdin>", line 1
sp = BeautifulSoup(html,
^
IndentationError: unexpected indent

parser)

File "<stdin>", line 1
parser)
^
IndentationError: unexpected indent

for tag in sp.find_all("a"):

File "<stdin>", line 1
for tag in sp.find_all("a"):
^
IndentationError: unexpected indent

url = tag.get("href")

File "<stdin>", line 1
url = tag.get("href")
^
IndentationError: unexpected indent

if url is None:

File "<stdin>", line 1
if url is None:
^
IndentationError: unexpected indent

continue

File "<stdin>", line 1
continue
^
IndentationError: unexpected indent

if "html" in url:

File "<stdin>", line 1
if "html" in url:
^
IndentationError: unexpected indent

print("\n" + url)

File "<stdin>", line 1
print("\n" + url)
^
IndentationError: unexpected indent

news = "https://news.google.com/"
Scraper(news).scrape()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Scraper instance has no attribute 'scrape'

結果は以上のようになりました。

発生している問題・エラーメッセージ

エラーメッセージ ```ImportError: No module named request ### 該当のソースコード >>> import urllib.request Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named request ### 補足情報(FW/ツールのバージョンなど) python3

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

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

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

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

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

m.ts10806

2018/10/19 22:54

タイトルに具体的な要件が含まれていないのでもう少し具体的に質問内容に寄せたタイトルにしていただけませんか?「初心者アイコン」も活用して、なるべく要件だけを書くようにしてください
m.ts10806

2018/10/19 22:57

質問テンプレート部分はそのままではなく、なるべく埋めてください。使わない項目は削られたほうが良いです。あと、現在のコード全体を提示されたほうが回答が得られやすくなると思います。
jun68ykt

2018/10/20 00:10

お使いの Python のバージョンは何になりますでしょうか? 2系か3系かを知りたいです。
ShunsukeNakao

2018/10/20 00:15

ご教示ありがとうございます。修正いたしました。
hayataka2049

2018/10/20 03:40

質問文を見る限り対話的インタプリタから実行している気がしますが、スクリプトとしてファイルに保存して実行した方が余計なエラーは取れると思います。
guest

回答2

0

Python3に移行し忘れていた

投稿2018/11/08 05:58

ShunsukeNakao

総合スコア13

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

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

0

ベストアンサー

こんにちは。

完全に問題を解決できるかは分かりませんが、お使いの Python のバージョンが3系とのことですので、とりあえず、以下

shell

1pip3 install urllib3

をやってみてから、再度プログラムを実行してみるといかがでしょうか?

投稿2018/10/20 00:23

編集2018/10/20 00:28
jun68ykt

総合スコア9058

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

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

ShunsukeNakao

2018/10/20 02:50

すみません、どうやら3系にアップデートできてなかったようです。 brew doctorをすると以下の警告が出てきました。 Please note that these warnings are just used to help the Homebrew maintainers with debugging if you file an issue. If everything you use Homebrew for is working fine: please don't worry or file an issue; just ignore this. Thanks! Warning: "config" scripts exist outside your system or Homebrew directories. `./configure` scripts often look for *-config scripts to determine if software packages are installed, and what additional flags to use when compiling and linking. Having additional scripts in your path can confuse software installed via Homebrew if the config script overrides a system or Homebrew provided script of the same name. We found the following "config" scripts: /Library/Frameworks/Python.framework/Versions/3.7/bin/python3-config Warning: Broken symlinks were found. Remove them with `brew prune`: /usr/local/bin/python3-config /usr/local/bin/python3.7-config /usr/local/bin/python3.7m-config よく分からず色々いじってしまってよく分からなくなってます。 どうやったら警告無くなりますか?
jun68ykt

2018/10/20 10:49

以下のご質問 > よく分からず色々いじってしまってよく分からなくなってます。 > どうやったら警告無くなりますか? については、brew の使い方もからむので、別途のご質問として投稿して、私だけではなく、他の多くの回答者さまからの回答をお待ちしたほうが、ShunsukeNakaoさんの問題解決につながりやすいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問