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

質問編集履歴

2

python3.5のスクリプトに改変しました、再度御教示お願いいたします。

2018/02/21 02:57

投稿

akakage13
akakage13

スコア89

title CHANGED
File without changes
body CHANGED
@@ -73,4 +73,115 @@
73
73
  と、エラーが出てきました。Typeerrorについて調べましたが、うまく動きませんでした。
74
74
 
75
75
 
76
- 二行めの16 を取る方法を 御教示 よろしくお願いいたします。
76
+ 二行めの16 を取る方法を 御教示 よろしくお願いいたします。
77
+
78
+
79
+
80
+
81
+
82
+
83
+ umyu様、丁寧な御教示ありがとうございます。
84
+
85
+ うまく動きました。ただ、
86
+
87
+ ご助言にもございましたが、小生、python3.5に移行中でございます。
88
+
89
+ そのため、今回のスクリプトをpython3.5に改変しましたところ、
90
+
91
+ 以下のようなスクリプトになりました。
92
+
93
+ ```ここに言語を入力
94
+ # -*- coding:utf-8 -*-
95
+
96
+ import urllib.request
97
+ import codecs
98
+ import time
99
+ from bs4 import BeautifulSoup
100
+
101
+ f1 = codecs.open('tokyo1.csv', 'w', 'utf-8')
102
+ f1.write('other_race_name'+u"\n")
103
+
104
+ url_1='http://race.netkeiba.com/?pid=race_old&id=c201805010801'
105
+
106
+ soup_1 = BeautifulSoup(urllib.request.urlopen(url_1), "html.parser")
107
+
108
+ #race1
109
+ tr_arr_1 = soup_1.select("table.race_table_old > tr ")
110
+ for tr_1 in tr_arr_1:
111
+ #time.sleep(0.25)
112
+ tds_1 = tr_1.findAll("td")
113
+ if len( tds_1 ) > 1:
114
+
115
+ other_race_name_tag_1 = soup_1.find('div',{'class':'race_otherdata'}).find('p')
116
+ other_race_name_1 = "".join([x for x in other_race_name_tag_1.text if not x == u'\xa0' and not x == u'\n'])
117
+ cols = [other_race_name_1.strip()]
118
+ f1.write(",".join(cols) + "\n")
119
+
120
+ print (other_race_name_1.strip())
121
+
122
+ f1.close()
123
+
124
+ ```
125
+
126
+
127
+
128
+ 上記のスクリプトは一行目まではうまく動きます。
129
+
130
+ しかしながら、今回の目的の二行目までを取得しようとして、
131
+
132
+ 御教示いただいたように、下記の通りに改変しましたが、うまく動きませんでした。
133
+
134
+
135
+ ```ここに言語を入力
136
+ # -*- coding:utf-8 -*-
137
+
138
+ import urllib.request
139
+ import codecs
140
+ import time
141
+ from bs4 import BeautifulSoup
142
+
143
+ f1 = codecs.open('tokyo1.csv', 'w', 'utf-8')
144
+ f1.write('other_race_name'+u"\n")
145
+
146
+ url_1='http://race.netkeiba.com/?pid=race_old&id=c201805010801'
147
+
148
+ soup_1 = BeautifulSoup(urllib.request.urlopen(url_1), "html.parser")
149
+
150
+ #race1
151
+ tr_arr_1 = soup_1.select("table.race_table_old > tr ")
152
+ for tr_1 in tr_arr_1:
153
+ #time.sleep(0.25)
154
+ tds_1 = tr_1.findAll("td")
155
+ if len( tds_1 ) > 1:
156
+
157
+ other_race_name_tag_1 = soup_1.find('div',{'class':'race_otherdata'}).find_all('p')
158
+ other_race_name_1 = "".join([x for x in other_race_name_tag_1.text if not x == u'\xa0' and not x == u'\n'])
159
+ cols = [other_race_name_1.strip()]
160
+ f1.write(",".join(cols) + "\n")
161
+
162
+ print (other_race_name_1.strip())
163
+
164
+ f1.close()
165
+ ```
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+
174
+ 下記のエラーが出てきました。
175
+
176
+ ```ここに言語を入力
177
+ Traceback (most recent call last):
178
+ File "C:\Users\satoru\satoru_system\race_data_scan\tokyo\1_tokyo_race_data_scan.py", line 23, in <module>
179
+ other_race_name_1 = "".join([x for x in other_race_name_tag_1.text if not x == u'\xa0' and not x == u'\n'])
180
+ File "C:\Users\satoru\AppData\Local\Programs\Python\Python36\lib\site-packages\bs4\element.py", line 1807, in __getattr__
181
+ "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
182
+ AttributeError: ResultSet object has no attribute 'text'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?```
183
+
184
+
185
+ python3.5におけるfindの使い方について、再度ご助言頂けますと助かります。
186
+
187
+ よろしくお願いいたします。

1

試したことと、うまく動かない結果について追記させていただきました。

2018/02/21 02:57

投稿

akakage13
akakage13

スコア89

title CHANGED
File without changes
body CHANGED
@@ -27,10 +27,50 @@
27
27
  ```
28
28
 
29
29
 
30
- 上記のソーコードでは、一行目の 1回 東京3日目 3歳 は 取れるのですが、
30
+ 上記のスクリプトでは、一行目の 1回 東京3日目 3歳 は 取れるのですが、
31
31
 
32
32
  目的とする、二行めの、 16頭 が 取ることが出来ず、苦慮しております。
33
33
 
34
- 二行目を取る方法をいろいろ調べましたが、うまく出来ませんでした。
35
34
 
35
+ 試したこと
36
+
37
+ 二行目を取る方法として、当該ソースコードを 見てみましたところ以下のような構造でした。
38
+
39
+ ```ここに言語を入力
40
+ 1R
41
+ </dt>
42
+ <dd>
43
+ <h1>3歳未勝利</h1>
44
+ <p><span>ダ1300m&nbsp;/&nbsp;天気:晴&nbsp;/&nbsp;馬場:不良&nbsp;/&nbsp;発走:11:00</span></p>
45
+ </dd>
46
+ </dl>
47
+ <div class="race_otherdata">
48
+ <p>1回東京3日目&nbsp;3歳&nbsp;</p>
49
+ <p>牝[指定]&nbsp;16頭</p>
50
+ <p>本賞金:500、200、130、75、50万円</p>
51
+ </div>
52
+ <ul class="btn_link_list fc">
53
+ ```
54
+
55
+
56
+ 上記の 1回 東京3日目 3歳はとれるので、<p> </p>で囲まれている全てを取ろうと考えて、上記のスクリプトのfindの箇所を
57
+
58
+ ```ここに言語を入力
59
+ other_race_name_tag_1 = soup_1.find('div',{'class':'race_otherdata'}).findall('p')
60
+ other_race_name_1 = "".join([x for x in other_race_name_tag_1.text if not x == u'\xa0' and not x == u'\n'])
61
+ ```
62
+
63
+
64
+ findall に改変して試しましたが、
65
+
66
+ ```ここに言語を入力
67
+ other_race_name_tag_1 = soup_1.find('div',{'class':'race_otherdata'}).findall('p')
68
+ TypeError: 'NoneType' object is not callable
69
+ ```
70
+
71
+
72
+
73
+ と、エラーが出てきました。Typeerrorについて調べましたが、うまく動きませんでした。
74
+
75
+
36
76
  二行めの16 を取る方法を 御教示 よろしくお願いいたします。