質問編集履歴

2

コード訂正

2016/10/14 09:01

投稿

cir
cir

スコア19

test CHANGED
File without changes
test CHANGED
@@ -22,9 +22,9 @@
22
22
 
23
23
  ###該当のソースコード
24
24
 
25
- ``Python
26
25
 
27
26
 
27
+ ```Python
28
28
 
29
29
  import csv
30
30
 
@@ -66,6 +66,12 @@
66
66
 
67
67
 
68
68
 
69
+ ```
70
+
71
+
72
+
73
+
74
+
69
75
  ###試したこと
70
76
 
71
77
  for文(for food in soup.findAll(class_='pc2015-item-inner pc2015-for-member'):)はprintするとすべての情報を書き出すことができるのでfor文は間違ってはいないと思います。

1

質問内容の変更

2016/10/14 09:01

投稿

cir
cir

スコア19

test CHANGED
File without changes
test CHANGED
@@ -2,13 +2,19 @@
2
2
 
3
3
  プログラミング初心者です。ご教授お願いします。
4
4
 
5
- イオンネットスーパー(https://www.aeonnetshop.com/shop/r/r5603_n01050000001160_v80/)での野菜の商品名と値段のスクレイピング結果をcsvに書き出したい。
5
+ イオンネットスーパー(https://www.aeonnetshop.com/shop/r/r5603_n01050000001160_v80/)でのすべての野菜の商品名と値段のスクレイピング結果をcsvに書き出したい。
6
+
7
+
8
+
9
+
6
10
 
7
11
  ###発生している問題・エラーメッセージ
8
12
 
9
- スクレイピング結果がリスト型データになっておらず、csvに書き出すことができない。
13
+ ホームページ上80種の野菜のうち80番目の野菜のみcsvに書き出すことができているが、他の79種の野菜の情報が書き出されない。
10
14
 
11
- ```
15
+
16
+
17
+
12
18
 
13
19
 
14
20
 
@@ -19,6 +25,8 @@
19
25
  ``Python
20
26
 
21
27
 
28
+
29
+ import csv
22
30
 
23
31
  import requests
24
32
 
@@ -36,10 +44,32 @@
36
44
 
37
45
  for food in soup.findAll(class_='pc2015-item-inner pc2015-for-member'):
38
46
 
39
- print(food.find(class_='pc2015-item-name').find('span').string)
47
+ food.find(class_='pc2015-item-name').find('span').string + food.find(class_='pc2015-price-1').find('strong').contents[0] + food.find(class_='pc2015-price-2').find('strong').contents[0]
40
48
 
41
- print(food.find(class_='pc2015-price-1').find('strong').contents[0])
49
+
42
50
 
43
- print(food.find(class_='pc2015-price-2').find('strong').contents[0])
51
+ header = ['name','price1','price2']
44
52
 
53
+ body = [
54
+
55
+ [food.find(class_='pc2015-item-name').find('span').string,food.find(class_='pc2015-price-1').find('strong').contents[0],food.find(class_='pc2015-price-2').find('strong').contents[0]]
56
+
57
+ ]
58
+
59
+ with open('food.csv', 'wt',newline='') as csvfile:
60
+
61
+ writer = csv.writer(csvfile)
62
+
63
+ writer.writerow(header)
64
+
65
+ writer.writerows(body)
66
+
67
+
68
+
69
+ ###試したこと
70
+
71
+ for文(for food in soup.findAll(class_='pc2015-item-inner pc2015-for-member'):)はprintするとすべての情報を書き出すことができるのでfor文は間違ってはいないと思います。
72
+
73
+ ###補足情報(言語/FW/ツール等のバージョンなど)
74
+
45
- ```
75
+ より詳細な情報