質問するログイン新規登録

質問編集履歴

5

2017/05/21 14:42

投稿

DaichiYasuda
DaichiYasuda

スコア173

title CHANGED
File without changes
body CHANGED
@@ -56,4 +56,4 @@
56
56
  fetched_dataframes = pandas.io.html.read_html(shop)
57
57
  fetched_dataframes[3].to_csv(shop_name)
58
58
 
59
- shop_nameというテキストファイルが作れない場合があります。
59
+ shop_nameというcsvファイルが作れない場合があります。

4

2017/05/21 14:42

投稿

DaichiYasuda
DaichiYasuda

スコア173

title CHANGED
File without changes
body CHANGED
@@ -50,4 +50,10 @@
50
50
 
51
51
  ```
52
52
 
53
- 例外処理が発生するのが
53
+ 例外処理が発生するのが下記の場所です
54
+ for shop_temp in shop_span.find_all('a'):
55
+ shop_name = shop_temp.text.strip()
56
+ fetched_dataframes = pandas.io.html.read_html(shop)
57
+ fetched_dataframes[3].to_csv(shop_name)
58
+
59
+ shop_nameというテキストファイルが作れない場合があります。

3

2017/05/21 14:40

投稿

DaichiYasuda
DaichiYasuda

スコア173

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,5 @@
1
1
  下記のpythonのコードでexceptで例外処理をしています。
2
- 例外処理をしたあとの中をもう一度実行したいのですがどうすればよろしいでしょうか???
2
+ 例外処理をしたあとのwhileの中をもう一度実行したいのですがどうすればよろしいでしょうか???
3
3
 
4
4
  ```python
5
5
  from selenium import webdriver
@@ -9,16 +9,14 @@
9
9
  import webbrowser
10
10
  import time
11
11
 
12
- #Chrome
13
- #driver = webdriver.Chrome('/home/daichi/chromedriver')
14
12
  driver = webdriver.PhantomJS()
15
13
  driver.set_window_size(1024, 768)
16
- driver.get('http://tabelog.com')
14
+ driver.get('http://example.com')
17
15
 
18
16
 
19
- input = driver.find_element_by_id("sa").send_keys(u"銀座駅")
17
+ input = driver.find_element_by_id("sa").send_keys(u"test")
20
18
  time.sleep(3)
21
- input = driver.find_element_by_id("sk").send_keys(u"フレンチ")
19
+ input = driver.find_element_by_id("sk").send_keys(u"test")
22
20
  btn = driver.find_element_by_id("js-global-search-btn").click()
23
21
 
24
22
  def get_data():

2

henkou

2017/05/21 14:22

投稿

DaichiYasuda
DaichiYasuda

スコア173

title CHANGED
File without changes
body CHANGED
@@ -1,8 +1,29 @@
1
1
  下記のpythonのコードでexceptで例外処理をしています。
2
- 例外処理をしたあとtryの中をもう一度実行したいのですがどうすればよろしいでしょうか???
2
+ 例外処理をしたあとの中をもう一度実行したいのですがどうすればよろしいでしょうか???
3
3
 
4
4
  ```python
5
+ from selenium import webdriver
6
+ import chardet
7
+ from bs4 import BeautifulSoup
8
+ import pandas
9
+ import webbrowser
10
+ import time
11
+
12
+ #Chrome
13
+ #driver = webdriver.Chrome('/home/daichi/chromedriver')
14
+ driver = webdriver.PhantomJS()
15
+ driver.set_window_size(1024, 768)
16
+ driver.get('http://tabelog.com')
17
+
18
+
19
+ input = driver.find_element_by_id("sa").send_keys(u"銀座駅")
20
+ time.sleep(3)
21
+ input = driver.find_element_by_id("sk").send_keys(u"フレンチ")
22
+ btn = driver.find_element_by_id("js-global-search-btn").click()
23
+
24
+ def get_data():
5
25
  while True:
26
+ try:
6
27
  data = driver.page_source.encode()
7
28
  soup = BeautifulSoup(data, 'html.parser')
8
29
  for tables in soup.find_all('div', attrs={'class': 'list-rst__rst-name'}):
@@ -13,20 +34,22 @@
13
34
  shop_src = BeautifulSoup(shop_data, 'html.parser')
14
35
  shop_list = shop_src.find('div', attrs={'class': 'rdheader-rstname'})
15
36
  for shop_span in shop_list.find_all('h2', attrs={'class': 'display-name'}):
16
- try:
17
- for shop_temp in shop_span.find_all('a'):
37
+ for shop_temp in shop_span.find_all('a'):
18
- shop_name = shop_temp.text.strip()
38
+ shop_name = shop_temp.text.strip()
19
- fetched_dataframes = pandas.io.html.read_html(shop)
39
+ fetched_dataframes = pandas.io.html.read_html(shop)
20
40
  fetched_dataframes[3].to_csv(shop_name)
21
- except Exception:
22
- f = open('failed.txt', 'a', newline='\n')
23
- f.write(shop_name)
24
- f.write('\n')
25
- f.write(shop)
26
- f.write('\n')
27
41
  driver.back()
28
42
  next = driver.find_element_by_link_text(u"次の20件")
29
43
  next.click()
44
+ except:
45
+ f = open('failed.txt', 'a', newline='\n')
46
+ f.write(shop_name)
47
+ f.write('\n')
48
+ f.write(shop)
49
+ f.write('\n')
50
+
51
+ get_data()
52
+
30
53
  ```
31
54
 
32
55
  例外処理が発生するのが

1

誤字

2017/05/21 14:21

投稿

DaichiYasuda
DaichiYasuda

スコア173

title CHANGED
File without changes
body CHANGED
@@ -3,7 +3,6 @@
3
3
 
4
4
  ```python
5
5
  while True:
6
- try:
7
6
  data = driver.page_source.encode()
8
7
  soup = BeautifulSoup(data, 'html.parser')
9
8
  for tables in soup.find_all('div', attrs={'class': 'list-rst__rst-name'}):
@@ -14,17 +13,20 @@
14
13
  shop_src = BeautifulSoup(shop_data, 'html.parser')
15
14
  shop_list = shop_src.find('div', attrs={'class': 'rdheader-rstname'})
16
15
  for shop_span in shop_list.find_all('h2', attrs={'class': 'display-name'}):
16
+ try:
17
- for shop_temp in shop_span.find_all('a'):
17
+ for shop_temp in shop_span.find_all('a'):
18
- shop_name = shop_temp.text.strip()
18
+ shop_name = shop_temp.text.strip()
19
- fetched_dataframes = pandas.io.html.read_html(shop)
19
+ fetched_dataframes = pandas.io.html.read_html(shop)
20
20
  fetched_dataframes[3].to_csv(shop_name)
21
+ except Exception:
22
+ f = open('failed.txt', 'a', newline='\n')
23
+ f.write(shop_name)
24
+ f.write('\n')
25
+ f.write(shop)
26
+ f.write('\n')
21
27
  driver.back()
22
28
  next = driver.find_element_by_link_text(u"次の20件")
23
29
  next.click()
24
- except:
25
- f = open('failed.txt', 'a', newline='\n')
26
- f.write(shop_name)
27
- f.write('\n')
28
- f.write(shop)
29
- f.write('\n')
30
- ```
30
+ ```
31
+
32
+ 例外処理が発生するのが