質問編集履歴

2

タグ文字を削除して半角スペース文字4つをインデントにして浄書いたしました。

2016/08/15 10:54

投稿

akakage13
akakage13

スコア89

test CHANGED
File without changes
test CHANGED
@@ -24,9 +24,13 @@
24
24
 
25
25
  Traceback (most recent call last):
26
26
 
27
- File "C:\Users\satoru\horse\kami-2.py", line 48, in <module>
27
+ File "C:\Users\satoru\horse\kami-2.py", line 46, in <module>
28
28
 
29
- print j_name,race_date, kaisai,weather,race_name,tousuu,frame_number,horse_number,\
29
+ print jockey_name,race_date,kaisai,weather,race_name,tousuu,frame_number,horse_number,single_win
30
+
31
+ _racio,popularity,horse_arrival,horse_name,weight,distance,baba,race_time,difference,horse_pass,pace
32
+
33
+ ,nobori,horse_weight,win_horse
30
34
 
31
35
  UnicodeEncodeError: 'cp932' codec can't encode character u'\xa0' in position 4: illegal multibyte se
32
36
 
@@ -52,7 +56,7 @@
52
56
 
53
57
  from bs4 import BeautifulSoup
54
58
 
55
- f = codecs.open('horse.csv', 'w', 'utf-8')
59
+ f = codecs.open('horse.csv','w','utf-8')
56
60
 
57
61
  f.write('jockey_name,race_date,kaisai,weather,race_number,race_name,tousuu,frame_number,horse_number,single_win_ratio,popularity,horse_arrival,horse_name,weight,distance,baba, race_time,difference,horse_pass,pace,nobori,horse_weight,win_horse'+ u"\n")
58
62
 
@@ -62,99 +66,83 @@
62
66
 
63
67
 
64
68
 
65
- for i in xrange(1, 2):
69
+ for i in xrange(1,2):
66
70
 
67
- url=tpl_url.format( i )
71
+ url=tpl_url.format( i )
68
72
 
69
- soup = BeautifulSoup(urllib2.urlopen(url).read(),"lxml")
73
+ soup = BeautifulSoup(urllib2.urlopen(url).read(),"lxml")
70
74
 
71
75
 
72
76
 
73
- j_name = soup.find('div', {'class':'db_head_name'}).find('h1').text.replace("\n", "")
77
+ jockey_name = soup.find('div',{'class':'db_head_name'}).find('h1').text.replace("\n","")
74
-
75
-
76
-
77
- tr_arr = soup.select("table.race_table_01 > tbody > tr")
78
-
79
-
80
-
81
- for tr in tr_arr:
82
-
83
-
84
-
85
- tds = tr.findAll("td")
86
-
87
- time.sleep(1)
88
-
89
- race_date = tds[0].a.text #日付
90
-
91
- kaisai = tds[1].a.text #開催
92
-
93
- weather=tds[2].text #天気
94
-
95
- race_number=tds[3].text #R
96
-
97
- race_name=tds[4].a.text #レース名
98
-
99
- tousuu=tds[6].text #頭数
100
-
101
- frame_number=tds[7].text #枠番
102
-
103
- horse_number=tds[8].text #馬番
104
-
105
- single_win_racio=tds[9].text #単勝
106
-
107
- popularity=tds[10].text #人気
108
-
109
- horse_arrival=tds[11].text #着順
110
-
111
- horse_name=tds[12].a.text #馬名
112
-
113
- weight=tds[13].text #斤量
114
-
115
- distance=tds[14].text #距離
116
-
117
- baba=tds[15].text #馬場
118
-
119
- race_time=tds[16].text #タイム
120
-
121
- difference=tds[17].text #着差
122
-
123
- horse_pass=tds[18].text #通過
124
-
125
- pace=tds[19].text #ペース
126
-
127
- nobori=tds[20].text #上り
128
-
129
- horse_weight=tds[21].text #馬体重
130
-
131
- win_horse=tds[22].a.text #勝ち馬
132
-
133
-
134
-
135
- print j_name,race_date, kaisai,weather,race_name,tousuu,frame_number,horse_number,\
136
-
137
- single_win_racio,popularity,horse_arrival,horse_name,weight,distance,baba,\
138
-
139
- race_time,difference,horse_pass,pace,nobori,horse_weight,win_horse
140
78
 
141
79
 
142
80
 
143
- cols = [j_name,race_date, kaisai,weather,race_number,race_name,tousuu,frame_number,\
81
+ tr_arr = soup.select("table.race_table_01>tbody>tr")
144
82
 
145
- horse_number,single_win_racio,popularity,horse_arrival,horse_name,weight,\
83
+ for tr in tr_arr:
146
84
 
147
- distance,baba,race_time,difference,horse_pass,pace,nobori,horse_weight,win_horse]
85
+ tds = tr.findAll("td")
148
86
 
149
-
87
+ time.sleep(1)
150
88
 
151
-
89
+ race_date = tds[0].a.text #日付
152
90
 
153
-
91
+ kaisai = tds[1].a.text #開催
154
92
 
155
- f.write(",".join(cols) + "\n")
93
+ weather=tds[2].text #天気
156
94
 
157
-
95
+ race_number=tds[3].text #R
96
+
97
+ race_name=tds[4].a.text #レース名
98
+
99
+ tousuu=tds[6].text #頭数
100
+
101
+ frame_number=tds[7].text #枠番
102
+
103
+ horse_number=tds[8].text #馬番
104
+
105
+ single_win_racio=tds[9].text #単勝
106
+
107
+ popularity=tds[10].text #人気
108
+
109
+ horse_arrival=tds[11].text #着順
110
+
111
+ horse_name=tds[12].a.text #馬名
112
+
113
+ weight=tds[13].text #斤量
114
+
115
+ distance=tds[14].text #距離
116
+
117
+ baba=tds[15].text #馬場
118
+
119
+ race_time=tds[16].text #タイム
120
+
121
+ difference=tds[17].text #着差
122
+
123
+ horse_pass=tds[18].text #通過
124
+
125
+ pace=tds[19].text #ペース
126
+
127
+ nobori=tds[20].text #上り
128
+
129
+ horse_weight=tds[21].text #馬体重
130
+
131
+ win_horse=tds[22].a.text #勝ち馬
132
+
133
+
134
+
135
+ print jockey_name,race_date,kaisai,weather,race_name,tousuu,frame_number,horse_number,single_win_racio,popularity,horse_arrival,horse_name,weight,distance,baba,race_time,difference,horse_pass,pace,nobori,horse_weight,win_horse
136
+
137
+
138
+
139
+ cols = [jockey_name,race_date,kaisai,weather,race_number,race_name,tousuu,frame_number,horse_number,single_win_racio,popularity,horse_arrival,horse_name,weight,distance,baba,race_time,difference,horse_pass,pace,nobori,horse_weight,win_horse]
140
+
141
+
142
+
143
+ f.write(",".join(cols) + "\n")
144
+
145
+
158
146
 
159
147
  f.close()
160
148
 

1

エラーメッセージの更新、ソースコードの訂正、インデントの訂正(しかしながらうまく貼り付けることが出来ませんでした、申し訳ございません。)タグの訂正

2016/08/15 10:54

投稿

akakage13
akakage13

スコア89

test CHANGED
File without changes
test CHANGED
@@ -20,23 +20,17 @@
20
20
 
21
21
  ```
22
22
 
23
- <h1>
24
-
25
- 陋ッ蜷肴ュ」鄒ゥツ
26
-
27
- (繧ィ繝薙リ繝槭し繝ィ繧キ)
28
-
29
- </h1> 2016/08/07 2新潟4 晴 3歳上500万下 15 7 13 6.9 3 2 チェイスダウン 54 ダ1200 良 1:11.9 0.3 8-8 3
30
-
31
- 4.5-37.1 36.6 468(+2) クリムゾンバローズ
23
+ C:\Users\satoru\horse>kami-2.py
32
24
 
33
25
  Traceback (most recent call last):
34
26
 
35
- File "C:\Users\satoru\horse\kami-2.py", line 58, in <module>
27
+ File "C:\Users\satoru\horse\kami-2.py", line 48, in <module>
36
28
 
37
- f.write(",".join(cols) + "\n")
29
+ print j_name,race_date, kaisai,weather,race_name,tousuu,frame_number,horse_number,\
38
30
 
39
- TypeError: sequence item 0: expected string, Tag found
31
+ UnicodeEncodeError: 'cp932' codec can't encode character u'\xa0' in position 4: illegal multibyte se
32
+
33
+ quence
40
34
 
41
35
  ```
42
36
 
@@ -76,7 +70,7 @@
76
70
 
77
71
 
78
72
 
79
- j_name = soup.find('div', {'class':'db_head_name'}).find('h1') #騎手の名前
73
+ j_name = soup.find('div', {'class':'db_head_name'}).find('h1').text.replace("\n", "")
80
74
 
81
75
 
82
76
 
@@ -88,69 +82,69 @@
88
82
 
89
83
 
90
84
 
91
- tds = tr.findAll("td")
85
+ tds = tr.findAll("td")
92
86
 
93
- time.sleep(1)
87
+ time.sleep(1)
94
88
 
95
- race_date = tds[0].a.text #日付
89
+ race_date = tds[0].a.text #日付
96
90
 
97
- kaisai = tds[1].a.text #開催
91
+ kaisai = tds[1].a.text #開催
98
92
 
99
- weather=tds[2].text #天気
93
+ weather=tds[2].text #天気
100
94
 
101
- race_number=tds[3].text #R
95
+ race_number=tds[3].text #R
102
96
 
103
- race_name=tds[4].a.text #レース名
97
+ race_name=tds[4].a.text #レース名
104
98
 
105
- tousuu=tds[6].text #頭数
99
+ tousuu=tds[6].text #頭数
106
100
 
107
- frame_number=tds[7].text #枠番
101
+ frame_number=tds[7].text #枠番
108
102
 
109
- horse_number=tds[8].text #馬番
103
+ horse_number=tds[8].text #馬番
110
104
 
111
- single_win_racio=tds[9].text #単勝
105
+ single_win_racio=tds[9].text #単勝
112
106
 
113
- popularity=tds[10].text #人気
107
+ popularity=tds[10].text #人気
114
108
 
115
- horse_arrival=tds[11].text #着順
109
+ horse_arrival=tds[11].text #着順
116
110
 
117
- horse_name=tds[12].a.text #馬名
111
+ horse_name=tds[12].a.text #馬名
118
112
 
119
- weight=tds[13].text #斤量
113
+ weight=tds[13].text #斤量
120
114
 
121
- distance=tds[14].text #距離
115
+ distance=tds[14].text #距離
122
116
 
123
- baba=tds[15].text #馬場
117
+ baba=tds[15].text #馬場
124
118
 
125
- race_time=tds[16].text #タイム
119
+ race_time=tds[16].text #タイム
126
120
 
127
- difference=tds[17].text #着差
121
+ difference=tds[17].text #着差
128
122
 
129
- horse_pass=tds[18].text #通過
123
+ horse_pass=tds[18].text #通過
130
124
 
131
- pace=tds[19].text #ペース
125
+ pace=tds[19].text #ペース
132
126
 
133
- nobori=tds[20].text #上り
127
+ nobori=tds[20].text #上り
134
128
 
135
- horse_weight=tds[21].text #馬体重
129
+ horse_weight=tds[21].text #馬体重
136
130
 
137
- win_horse=tds[22].a.text #勝ち馬
131
+ win_horse=tds[22].a.text #勝ち馬
138
132
 
139
133
 
140
134
 
141
- print j_name,race_date, kaisai,weather,race_name,tousuu,frame_number,horse_number,\
135
+ print j_name,race_date, kaisai,weather,race_name,tousuu,frame_number,horse_number,\
142
136
 
143
- single_win_racio,popularity,horse_arrival,horse_name,weight,distance,baba,\
137
+ single_win_racio,popularity,horse_arrival,horse_name,weight,distance,baba,\
144
138
 
145
- race_time,difference,horse_pass,pace,nobori,horse_weight,win_horse
139
+ race_time,difference,horse_pass,pace,nobori,horse_weight,win_horse
146
140
 
147
141
 
148
142
 
149
- cols = [j_name,race_date, kaisai,weather,race_number,race_name,tousuu,frame_number,\
143
+ cols = [j_name,race_date, kaisai,weather,race_number,race_name,tousuu,frame_number,\
150
144
 
151
- horse_number,single_win_racio,popularity,horse_arrival,horse_name,weight,\
145
+ horse_number,single_win_racio,popularity,horse_arrival,horse_name,weight,\
152
146
 
153
- distance,baba,race_time,difference,horse_pass,pace,nobori,horse_weight,win_horse]
147
+ distance,baba,race_time,difference,horse_pass,pace,nobori,horse_weight,win_horse]
154
148
 
155
149
 
156
150
 
@@ -158,7 +152,7 @@
158
152
 
159
153
 
160
154
 
161
- f.write(",".join(cols) + "\n")
155
+ f.write(",".join(cols) + "\n")
162
156
 
163
157
 
164
158