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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Python 3.x

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

Q&A

解決済

1回答

6267閲覧

jupyterでスクレイピングできない

masamos

総合スコア7

Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Python 3.x

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

0グッド

0クリップ

投稿2017/12/12 08:25

編集2017/12/12 08:28
from urllib.request import urlopen from urllib.error import HTTPError from bs4 import BeautifulSoup def getTitle(url): try: html = urlopen(url) except HTTPError as e: print(e) return None try: bs0bj = BeautifulSoup(html.read()) title = bs0bj.body.h1 except AttributeError as e: return None return title title = getTitle("http://www.pythonscraping.com/exercises/exercise1.html") if title == None: print('Title could not be found') else: print(title) ```###前提・実現したいこと jupyterでwebスクレイピングの練習をしたいのですが タイムアウトエラーが出る原因がわかりません。 コードの問題なのかパソコンの設定の問題なのか、 どなたか教えていただけませんか。 ###発生している問題・エラーメッセージ ###該当のソースコード ```ここに言語を入力 TimeoutError Traceback (most recent call last) ~\Anaconda3\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args) 1317 h.request(req.get_method(), req.selector, req.data, headers, -> 1318 encode_chunked=req.has_header('Transfer-encoding')) 1319 except OSError as err: # timeout error ~\Anaconda3\lib\http\client.py in request(self, method, url, body, headers, encode_chunked) 1238 """Send a complete request to the server.""" -> 1239 self._send_request(method, url, body, headers, encode_chunked) 1240 ~\Anaconda3\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked) 1284 body = _encode(body, 'body') -> 1285 self.endheaders(body, encode_chunked=encode_chunked) 1286 ~\Anaconda3\lib\http\client.py in endheaders(self, message_body, encode_chunked) 1233 raise CannotSendHeader() -> 1234 self._send_output(message_body, encode_chunked=encode_chunked) 1235 ~\Anaconda3\lib\http\client.py in _send_output(self, message_body, encode_chunked) 1025 del self._buffer[:] -> 1026 self.send(msg) 1027 ~\Anaconda3\lib\http\client.py in send(self, data) 963 if self.auto_open: --> 964 self.connect() 965 else: ~\Anaconda3\lib\http\client.py in connect(self) 935 self.sock = self._create_connection( --> 936 (self.host,self.port), self.timeout, self.source_address) 937 self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) ~\Anaconda3\lib\socket.py in create_connection(address, timeout, source_address) 723 if err is not None: --> 724 raise err 725 else: ~\Anaconda3\lib\socket.py in create_connection(address, timeout, source_address) 712 sock.bind(source_address) --> 713 sock.connect(sa) 714 # Break explicitly a reference cycle TimeoutError: [WinError 10060] 接続済みの呼び出し先が一定の時間を過ぎても正しく応答しなかったため、接続できませんでした。または接続済みのホストが応答しなかったため、確立された接続は失敗しました。 During handling of the above exception, another exception occurred: URLError Traceback (most recent call last) <ipython-input-13-b9f22ee773ba> in <module>() 16 return title 17 ---> 18 title = getTitle("http://www.pythonscraping.com/exercises/exercise1.html") 19 if title == None: 20 print('Title could not be found') <ipython-input-13-b9f22ee773ba> in getTitle(url) 5 def getTitle(url): 6 try: ----> 7 html = urlopen(url) 8 except HTTPError as e: 9 print(e) ~\Anaconda3\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context) 221 else: 222 opener = _opener --> 223 return opener.open(url, data, timeout) 224 225 def install_opener(opener): ~\Anaconda3\lib\urllib\request.py in open(self, fullurl, data, timeout) 524 req = meth(req) 525 --> 526 response = self._open(req, data) 527 528 # post-process response ~\Anaconda3\lib\urllib\request.py in _open(self, req, data) 542 protocol = req.type 543 result = self._call_chain(self.handle_open, protocol, protocol + --> 544 '_open', req) 545 if result: 546 return result ~\Anaconda3\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args) 502 for handler in handlers: 503 func = getattr(handler, meth_name) --> 504 result = func(*args) 505 if result is not None: 506 return result ~\Anaconda3\lib\urllib\request.py in http_open(self, req) 1344 1345 def http_open(self, req): -> 1346 return self.do_open(http.client.HTTPConnection, req) 1347 1348 http_request = AbstractHTTPHandler.do_request_ ~\Anaconda3\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args) 1318 encode_chunked=req.has_header('Transfer-encoding')) 1319 except OSError as err: # timeout error -> 1320 raise URLError(err) 1321 r = h.getresponse() 1322 except: URLError: <urlopen error [WinError 10060] 接続済みの呼び出し先が一定の時間を過ぎても正しく応答しなかったため、接続できませんでした。または接続済みのホストが応答しなかったため、確立された接続は失敗しました。>

###試したこと
課題に対してアプローチしたことを記載してください

###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報

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

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

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

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

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

guest

回答1

0

ベストアンサー

こちらの環境では正常に動作したのでコードに問題はありません。
環境に問題があると思われます。

  • そのパソコンからインターネットにはつながるか?
  • ブラウザからそのページは開けるか?
  • そのコードをtest.pyなりに保存してコマンドプロンプト上からpython test.pyと実行するとどうか?
  • 接続先をhttp://www.example.com/などではどうか?

などを確認、追試ください。

投稿2017/12/12 08:46

can110

総合スコア38258

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

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

masamos

2017/12/12 11:33

解決しました。どうもLANの問題で接続環境を変えるとできました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問