回答編集履歴

3

d

2018/11/01 06:47

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -24,15 +24,15 @@
24
24
 
25
25
  # スキー場名
26
26
 
27
- span_tag = section_tag.select('div[class="detailHead"] > span')[0]
27
+ span_tag = section_tag.select_one('div[class="detailHead"] > span')
28
28
 
29
29
  site = span_tag.string
30
30
 
31
-
31
+
32
32
 
33
33
  # コース数
34
34
 
35
- td_tag = section_tag.select('table[class="type1"] tr:nth-of-type(3) td')[0]
35
+ td_tag = section_tag.select_one('table[class="type1"] tr:nth-of-type(3) td')
36
36
 
37
37
  num_courses = td_tag.string
38
38
 

2

d

2018/11/01 06:47

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -32,7 +32,7 @@
32
32
 
33
33
  # コース数
34
34
 
35
- td_tag = section_tag.select('table[class="type1"] tr:nth-of-type(3) td:nth-of-type(1)')[0]
35
+ td_tag = section_tag.select('table[class="type1"] tr:nth-of-type(3) td')[0]
36
36
 
37
37
  num_courses = td_tag.string
38
38
 
@@ -108,7 +108,7 @@
108
108
 
109
109
  tr_tags = section_tag.find('table', class_='type1').find_all('tr')
110
110
 
111
- num_courses = tr_tags[2].find('td').string
111
+ num_courses = tr_tags[2].td.string
112
112
 
113
113
  print(site, num_courses)
114
114
 

1

d

2018/11/01 05:26

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  res.raise_for_status()
18
18
 
19
- soup = BeautifulSoup(res.content)
19
+ soup = BeautifulSoup(res.content, 'lxml')
20
20
 
21
21
 
22
22
 
@@ -67,3 +67,119 @@
67
67
  【北海道】エリア:函館周辺 8コース
68
68
 
69
69
  ```
70
+
71
+
72
+
73
+ ## 追記
74
+
75
+
76
+
77
+ CSS セレクタではなく、find_all() を使った場合
78
+
79
+
80
+
81
+ ```
82
+
83
+ import requests
84
+
85
+ from bs4 import BeautifulSoup
86
+
87
+
88
+
89
+ url = 'https://snownet.jp/search/?prefarea=1'
90
+
91
+ res = requests.get(url)
92
+
93
+ res.raise_for_status()
94
+
95
+ soup = BeautifulSoup(res.content, 'lxml')
96
+
97
+
98
+
99
+ for section_tag in soup.find_all('section', class_='skiDetail'):
100
+
101
+ # スキー場名
102
+
103
+ site = section_tag.find('div', class_='detailHead').span.string
104
+
105
+
106
+
107
+ # コース数
108
+
109
+ tr_tags = section_tag.find('table', class_='type1').find_all('tr')
110
+
111
+ num_courses = tr_tags[2].find('td').string
112
+
113
+ print(site, num_courses)
114
+
115
+ ```
116
+
117
+
118
+
119
+ section タグ以下が以下の階層構造になっていることを把握した上で、CSS セレクタまたは find_all(), find() を使い、タグを選んでいけばいいです。
120
+
121
+ コース数は table タグの子要素の3つ目の tr タグの子の td タグに貼っています。
122
+
123
+
124
+
125
+ なので、CSS セレクタの場合、`table[class="type1"] tr:nth-of-type(3) td:nth-of-type(1)` となります。
126
+
127
+ また、find() で選ぶ場合は、`section_tag.find('table', class_='type1').find_all('tr')[2].find('td')` となります。
128
+
129
+
130
+
131
+ ```
132
+
133
+ # 視認性のため、改行など空白の NavigableString は除いています。
134
+
135
+ section (Tag)
136
+
137
+ └── table (Tag)
138
+
139
+ ├── tr (Tag)
140
+
141
+ │ ├── th (Tag)
142
+
143
+ │ │ └── 'アクセス' (NavigableString)
144
+
145
+ │ └── td (Tag)
146
+
147
+ │ ├── '[車] 函館市内から36km' (NavigableString)
148
+
149
+ │ ├── br (Tag)
150
+
151
+ │ └── '[その他] 函館本線・大沼公園駅より車で15分' (NavigableString)
152
+
153
+ ├── tr (Tag)
154
+
155
+ │ ├── th (Tag)
156
+
157
+ │ │ └── '営業時間' (NavigableString)
158
+
159
+ │ └── td (Tag)
160
+
161
+ │ ├── '[平日] 09:00~17:00' (NavigableString)
162
+
163
+ │ ├── br (Tag)
164
+
165
+ │ ├── '[日祝] 09:00~17:00' (NavigableString)
166
+
167
+ │ ├── br (Tag)
168
+
169
+ │ ├── '[ナイター] 17:00~22:00' (NavigableString)
170
+
171
+ │ ├── br (Tag)
172
+
173
+ │ └── '営業時間 9:00〜17:00\u3000[ナイター] 17:00〜22:00※12/20〜3/8金・土・日・祝前日のみ' (NavigableString)
174
+
175
+ └── tr (Tag)
176
+
177
+ ├── th (Tag)
178
+
179
+ │ └── 'コース数' (NavigableString)
180
+
181
+ └── td (Tag)
182
+
183
+ └── '8コース' (NavigableString)
184
+
185
+ ```