質問編集履歴
1
大幅な概要修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Pythonによる
|
1
|
+
Pythonによるエラーの解消について
|
body
CHANGED
@@ -2,22 +2,22 @@
|
|
2
2
|
|
3
3
|
Pythonによるスクレイピング機能の実装を行っております。
|
4
4
|
概要と致しまして、url.txtから該当のurlから必要な情報を読み込み、csvへ書き出す作業になります。
|
5
|
-
しかし、
|
5
|
+
しかし、エラーにより実行ができません。
|
6
6
|
|
7
7
|
### 試したこと
|
8
|
+
(修正により)
|
8
9
|
|
10
|
+
大幅に記載する理由を変更致します。
|
9
|
-
|
11
|
+
インデックスエラーによる表記はなくなったのですが関数やtryなどの記載をしましたが構文エラーにより実行が行えなくなりました。
|
10
12
|
|
11
|
-
現状の知識でどのような修正により、改善できるか不明であり、教授して頂けないでしょうか。
|
12
13
|
|
13
|
-
|
14
14
|
### 発生している問題・エラーメッセージ
|
15
15
|
|
16
16
|
```
|
17
|
-
|
17
|
+
File "surugaya.py", line 14
|
18
|
-
|
18
|
+
for line in lines2:
|
19
|
-
|
19
|
+
^
|
20
|
-
|
20
|
+
SyntaxError: invalid syntax
|
21
21
|
```
|
22
22
|
|
23
23
|
### 該当のソースコード
|
@@ -27,36 +27,37 @@
|
|
27
27
|
import urllib.request as req
|
28
28
|
import csv
|
29
29
|
|
30
|
+
def startScrapy():
|
31
|
+
try:
|
30
|
-
f = open('url.txt')
|
32
|
+
f = open('url.txt')
|
31
|
-
lines2 = f.readlines()
|
33
|
+
lines2 = f.readlines()
|
32
|
-
f.close()
|
34
|
+
f.close()
|
35
|
+
pa = []
|
33
36
|
|
34
37
|
|
35
|
-
|
38
|
+
# 取得
|
39
|
+
for line in lines2:
|
40
|
+
url = "https://www.suruga-ya.jp/product/detail/" + line
|
41
|
+
res = req.urlopen(url)
|
42
|
+
soup = BeautifulSoup(res, 'html.parser')
|
36
43
|
|
37
|
-
# 取得
|
38
|
-
for line in lines2:
|
39
|
-
url = "https://www.suruga-ya.jp/product/detail/" + line
|
40
|
-
res = req.urlopen(url)
|
41
|
-
soup = BeautifulSoup(res, 'html.parser')
|
42
|
-
|
43
|
-
|
44
|
+
title1 = soup.select('#item_title')
|
44
|
-
|
45
|
+
if len(title1) > 0 :
|
45
|
-
|
46
|
+
for i in title1:
|
46
|
-
|
47
|
+
pt = i.get_text().replace('\n', '')
|
47
|
-
|
48
|
+
else:
|
48
|
-
|
49
|
+
pt = 'Nothing Product Name'
|
49
50
|
|
50
|
-
|
51
|
+
p_list = soup.select('#price')
|
51
|
-
|
52
|
+
if len(p_list) > 0 :
|
52
|
-
|
53
|
+
for x in p_list:
|
53
|
-
|
54
|
+
pl = x.get_text().replace('\n', '').replace('\r', '')
|
54
|
-
|
55
|
+
else:
|
55
|
-
|
56
|
+
pl = 'Nothing Product Price'
|
56
|
-
|
57
|
+
pa += [line, pt, pl, 0]
|
57
|
-
|
58
|
+
print(pa)
|
58
59
|
|
59
|
-
|
60
|
+
return pa
|
60
61
|
except:
|
61
62
|
print("失敗")
|
62
63
|
|
@@ -65,4 +66,7 @@
|
|
65
66
|
with open('price.csv', 'w', encoding='utf-8') as _file:
|
66
67
|
writer = csv.writer(_file, lineterminator='\n')
|
67
68
|
writer.writerow(pa)
|
69
|
+
|
70
|
+
|
71
|
+
|
68
72
|
```
|