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

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

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

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

Python

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

Q&A

解決済

1回答

44059閲覧

pythonでtry exceptでリトライする処理を導入したい

DaichiYasuda

総合スコア173

Python 3.x

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

Python

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

1グッド

4クリップ

投稿2017/05/21 13:58

編集2017/05/21 14:42

下記のpythonのコードでexceptで例外処理をしています。
例外処理をしたあとのwhileの中をもう一度実行したいのですがどうすればよろしいでしょうか???

python

1from selenium import webdriver 2import chardet 3from bs4 import BeautifulSoup 4import pandas 5import webbrowser 6import time 7 8driver = webdriver.PhantomJS() 9driver.set_window_size(1024, 768) 10driver.get('http://example.com') 11 12 13input = driver.find_element_by_id("sa").send_keys(u"test") 14time.sleep(3) 15input = driver.find_element_by_id("sk").send_keys(u"test") 16btn = driver.find_element_by_id("js-global-search-btn").click() 17 18def get_data(): 19 while True: 20 try: 21 data = driver.page_source.encode() 22 soup = BeautifulSoup(data, 'html.parser') 23 for tables in soup.find_all('div', attrs={'class': 'list-rst__rst-name'}): 24 for link in tables.find_all('a'): 25 shop =link.get('href') 26 driver.get(shop) 27 shop_data = driver.page_source.encode() 28 shop_src = BeautifulSoup(shop_data, 'html.parser') 29 shop_list = shop_src.find('div', attrs={'class': 'rdheader-rstname'}) 30 for shop_span in shop_list.find_all('h2', attrs={'class': 'display-name'}): 31 for shop_temp in shop_span.find_all('a'): 32 shop_name = shop_temp.text.strip() 33 fetched_dataframes = pandas.io.html.read_html(shop) 34 fetched_dataframes[3].to_csv(shop_name) 35 driver.back() 36 next = driver.find_element_by_link_text(u"次の20件") 37 next.click() 38 except: 39 f = open('failed.txt', 'a', newline='\n') 40 f.write(shop_name) 41 f.write('\n') 42 f.write(shop) 43 f.write('\n') 44 45get_data() 46

例外処理が発生するのが下記の場所です
for shop_temp in shop_span.find_all('a'):
shop_name = shop_temp.text.strip()
fetched_dataframes = pandas.io.html.read_html(shop)
fetched_dataframes[3].to_csv(shop_name)

shop_nameというcsvファイルが作れない場合があります。

Dokumoot👍を押しています

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

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

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

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

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

guest

回答1

0

ベストアンサー

python

1for _ in range(3): # 最大3回実行 2 try: 3 do_something() # 失敗しそうな処理 4 except Exception as e: 5 pass # 必要であれば失敗時の処理 6 else: 7 break # 失敗しなかった時はループを抜ける 8else: 9 pass # リトライが全部失敗した時の処理

投稿2017/05/21 15:25

YouheiSakurai

総合スコア6142

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問