質問編集履歴

1

大幅な概要修正

2018/06/06 05:22

投稿

rina0366
rina0366

スコア58

test CHANGED
@@ -1 +1 @@
1
- Pythonによるインデントエラーの解消について
1
+ Pythonによるエラーの解消について
test CHANGED
@@ -6,19 +6,19 @@
6
6
 
7
7
  概要と致しまして、url.txtから該当のurlから必要な情報を読み込み、csvへ書き出す作業になります。
8
8
 
9
- しかし、インデントエラーにより実行ができません。
9
+ しかし、エラーにより実行ができません。
10
10
 
11
11
 
12
12
 
13
13
  ### 試したこと
14
14
 
15
-
16
-
17
- 現状のエラーではインデント以外のエラー表記もでておりますが、先ほどインデントの調整を行っているときはインデントのみのときもありました、しかし、全体の構成を考えたときのインデントの配置を意識して現在のコード至る所存であますが、やはり何度調整してもインデントのエラーになってしまいます。
15
+ (修正)
18
16
 
19
17
 
20
18
 
19
+ 大幅に記載する理由を変更致します。
20
+
21
- 現状知識でどのような修正により、改善できるか不明であ、教授て頂けないでしょうか
21
+ インデックスエラーによる表記はなくなったのですが関数やtryなどの記載をしましたが構文エラーにより実行が行えなくな
22
22
 
23
23
 
24
24
 
@@ -30,13 +30,13 @@
30
30
 
31
31
  ```
32
32
 
33
- File "surugaya.py", line 35
33
+ File "surugaya.py", line 14
34
34
 
35
- except:
35
+ for line in lines2:
36
36
 
37
- ^
37
+ ^
38
38
 
39
- IndentationError: unexpected indent
39
+ SyntaxError: invalid syntax
40
40
 
41
41
  ```
42
42
 
@@ -56,65 +56,67 @@
56
56
 
57
57
 
58
58
 
59
- f = open('url.txt')
59
+ def startScrapy():
60
60
 
61
- lines2 = f.readlines()
61
+ try:
62
62
 
63
+ f = open('url.txt')
64
+
65
+ lines2 = f.readlines()
66
+
63
- f.close()
67
+ f.close()
68
+
69
+ pa = []
64
70
 
65
71
 
66
72
 
67
73
 
68
74
 
69
- pa = []
75
+ # 取得
76
+
77
+ for line in lines2:
78
+
79
+ url = "https://www.suruga-ya.jp/product/detail/" + line
80
+
81
+ res = req.urlopen(url)
82
+
83
+ soup = BeautifulSoup(res, 'html.parser')
70
84
 
71
85
 
72
86
 
73
- # 取得
87
+ title1 = soup.select('#item_title')
74
88
 
75
- for line in lines2:
89
+ if len(title1) > 0 :
76
90
 
77
- url = "https://www.suruga-ya.jp/product/detail/" + line
91
+ for i in title1:
78
92
 
79
- res = req.urlopen(url)
93
+ pt = i.get_text().replace('\n', '')
80
94
 
81
- soup = BeautifulSoup(res, 'html.parser')
95
+ else:
82
96
 
83
-
84
-
85
- title1 = soup.select('#item_title')
86
-
87
- if len(title1) > 0 :
88
-
89
- for i in title1:
90
-
91
- pt = i.get_text().replace('\n', '')
92
-
93
- else:
94
-
95
- pt = 'Nothing Product Name'
97
+ pt = 'Nothing Product Name'
96
98
 
97
99
 
98
100
 
99
- p_list = soup.select('#price')
101
+ p_list = soup.select('#price')
100
102
 
101
- if len(p_list) > 0 :
103
+ if len(p_list) > 0 :
102
104
 
103
- for x in p_list:
105
+ for x in p_list:
104
106
 
105
- pl = x.get_text().replace('\n', '').replace('\r', '')
107
+ pl = x.get_text().replace('\n', '').replace('\r', '')
106
108
 
107
- else:
109
+ else:
108
110
 
109
- pl = 'Nothing Product Price'
111
+ pl = 'Nothing Product Price'
110
112
 
111
- pa += [line, pt, pl, 0]
113
+ pa += [line, pt, pl, 0]
112
114
 
113
- print(pa)
115
+ print(pa)
114
116
 
115
117
 
116
118
 
117
- return pa
119
+ return pa
118
120
 
119
121
  except:
120
122
 
@@ -132,4 +134,10 @@
132
134
 
133
135
  writer.writerow(pa)
134
136
 
137
+
138
+
139
+
140
+
141
+
142
+
135
143
  ```