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

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

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

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

Q&A

解決済

1回答

2198閲覧

Pythonにて

kaitokimura

総合スコア59

Python

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

0グッド

0クリップ

投稿2017/05/03 12:31

例えばhttp://sample.com/a/b/c/d/e/1?ima=0000&cd=member
にブログの1ページ目があるとします。
2ページ目は
http://sample.com/a/b/c/d/e/2?ima=0000&cd=memberにあるとします。
このブログをBeautiful Soup 4を使って1ページ目から読み込みたいのですが
ときどき記事が削除されて404がでているときに
そのエラーを受け取ったら次のページにいきたいのですがうまく行きません。

lang

1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3from urllib.request import urlopen 4from urllib.error import URLError, HTTPError 5from bs4 import BeautifulSoup 6import os 7url = 'http://sample.com/a/b/c/d/e/{}?ima=0000&cd=member' 8try: 9 for i in range(1, 1000): 10 html = urlopen(url.format(str(i))) 11 soup = BeautifulSoup(html) 12 print(soup.find('div',{'class':'headArea'}).p.text) 13 14except HTTPError as e: 15 for i in range(i+1, 1000): 16 html = urlopen(url.format(str(i))) 17 soup = BeautifulSoup(html) 18 print('Error code: ', e.code) 19except URLError as e: 20 for i in range(i+1, 1000): 21 html = urlopen(url.format(str(i))) 22 soup = BeautifulSoup(html) 23 print('Reason: ', e.reason)

エラーメッセージ

lang

1Traceback (most recent call last): 2 File "sample.py", line 21, in <module> 3 html = urlopen(url.format(str(i))) 4 File "/Users/Mypc/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py", line 223, in urlopen 5 return opener.open(url, data, timeout) 6 File "/Users/Mypc/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py", line 532, in open 7 response = meth(req, response) 8 File "/Users/Mypc/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py", line 642, in http_response 9 'http', request, response, code, msg, hdrs) 10 File "/Users/Mypc/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py", line 570, in error 11 return self._call_chain(*args) 12 File "/Users/Mypc/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py", line 504, in _call_chain 13 result = func(*args) 14 File "/Users/Mypc/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py", line 650, in http_error_default 15 raise HTTPError(req.full_url, code, msg, hdrs, fp) 16urllib.error.HTTPError: HTTP Error 404: Not Found 17 18During handling of the above exception, another exception occurred: 19 20Traceback (most recent call last): 21 File "sample.py", line 27, in <module> 22 html = urlopen(url.format(str(i))) 23 File "/Users/Mypc/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py", line 223, in urlopen 24 return opener.open(url, data, timeout) 25 File "/Users/Mypc/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py", line 532, in open 26 response = meth(req, response) 27 File "/Users/Mypc/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py", line 642, in http_response 28 'http', request, response, code, msg, hdrs) 29 File "/Users/Mypc/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py", line 570, in error 30 return self._call_chain(*args) 31 File "/Users/Mypc/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py", line 504, in _call_chain 32 result = func(*args) 33 File "/Users/Mypc/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py", line 650, in http_error_default 34 raise HTTPError(req.full_url, code, msg, hdrs, fp) 35urllib.error.HTTPError: HTTP Error 404: Not Found

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

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

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

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

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

guest

回答1

0

ベストアンサー

tryexceptfor の中に入れましょう。

python

1for i in range(1, 1000): 2 try: 3 html = urlopen(url.format(str(i))) 4 except: 5 # 例外処理 6 else: 7 # 成功したときの処理

元サイトに迷惑がかかるので、適度にsleep処理をいれるべきです。

投稿2017/05/03 12:41

driller

総合スコア720

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

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

kaitokimura

2017/05/03 12:58

sleep処理かしこまりました。 '''UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently. The code that caused this warning is on line 33 of the file sample.py. To get rid of this warning, change code that looks like this: BeautifulSoup([your markup]) to this: BeautifulSoup([your markup], "lxml") markup_type=markup_type)) 5000 Traceback (most recent call last): File "sample.py", line 36, in <module> print('Reason: ', e.reason) NameError: name 'e' is not defined''' これはどういう意味でしょうか。。。
driller

2017/05/03 13:04

except hogehoge as e: のeを定義していないのではないでしょうか
kaitokimura

2017/05/04 07:04

except HTTPError as e:とexcept URLError as e:を設けてますがここではないですか??
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問