質問編集履歴
2
機能2を修正しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -194,18 +194,24 @@
|
|
194
194
|
|
195
195
|
url = link.get("href")
|
196
196
|
|
197
|
+
print(url)
|
198
|
+
|
197
199
|
for s_tag in soup.find_all("h3","shisetsu_name"):
|
198
200
|
|
199
201
|
s_link = s_tag.find("a")
|
200
202
|
|
201
203
|
s_url = s_link.get("href")
|
202
204
|
|
205
|
+
print(s_url)
|
206
|
+
|
203
207
|
links = soup.find_all("a","fa_name")
|
204
208
|
|
205
209
|
for link in links:
|
206
210
|
|
207
211
|
l_url = link.get('href')
|
208
212
|
|
213
|
+
print(l_url)
|
214
|
+
|
209
215
|
else:
|
210
216
|
|
211
217
|
break
|
1
動作するソースコード、動作しないソースコードをまとめました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -92,54 +92,190 @@
|
|
92
92
|
|
93
93
|
i += 1
|
94
94
|
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
【機能2】
|
100
|
+
|
101
|
+
def get_soup(url):
|
102
|
+
|
103
|
+
html = requests.get(url)
|
104
|
+
|
105
|
+
return BeautifulSoup(html.content, "html.parser")
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
def scraping_gh():
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
soup = get_soup(get_href())
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
# 整骨院の名称
|
118
|
+
|
119
|
+
res_p = soup.find("span", "name")
|
120
|
+
|
121
|
+
res = res_p.find(text=re.compile(""))
|
122
|
+
|
123
|
+
print(res.string)
|
124
|
+
|
125
|
+
# ホームページのURL
|
126
|
+
|
127
|
+
res_p = soup.find("p", "lnk_url")
|
128
|
+
|
129
|
+
res = res_p.find(text=re.compile(""))
|
130
|
+
|
131
|
+
print(res.string)
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
scraping_gh()
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
【機能1】【機能2】はそれぞれ動作するのですが、一緒にすると、エラーも表示されなくなってしまいました。
|
142
|
+
|
143
|
+
わかる方、教えていただけると幸いです。
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
### 動作するソースコード【機能1】
|
152
|
+
|
153
|
+
```jupyter
|
154
|
+
|
155
|
+
import sys
|
156
|
+
|
157
|
+
import requests
|
158
|
+
|
159
|
+
import re
|
160
|
+
|
161
|
+
import urllib.request, urllib.error
|
162
|
+
|
163
|
+
from bs4 import BeautifulSoup
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
def get_href():
|
168
|
+
|
169
|
+
i = 1
|
170
|
+
|
171
|
+
num = 2
|
172
|
+
|
173
|
+
while i < 48:
|
174
|
+
|
175
|
+
for num in range(1, 300):
|
176
|
+
|
177
|
+
zero_i = str(i).zfill(2)
|
178
|
+
|
179
|
+
base = 'https://www.judo-ch.jp/sekkotsuinsrch/{}/list/{}/'
|
180
|
+
|
181
|
+
url = base.format(zero_i,num)
|
182
|
+
|
183
|
+
res = requests.get(url)
|
184
|
+
|
185
|
+
if res.status_code == 200:
|
186
|
+
|
187
|
+
html = requests.get(url)
|
188
|
+
|
189
|
+
soup = BeautifulSoup(html.content,"html.parser")
|
190
|
+
|
191
|
+
for tag in soup.find_all("h3","shisetsu_name_s"):
|
192
|
+
|
193
|
+
link = tag.find("a")
|
194
|
+
|
195
|
+
url = link.get("href")
|
196
|
+
|
197
|
+
for s_tag in soup.find_all("h3","shisetsu_name"):
|
198
|
+
|
199
|
+
s_link = s_tag.find("a")
|
200
|
+
|
201
|
+
s_url = s_link.get("href")
|
202
|
+
|
203
|
+
links = soup.find_all("a","fa_name")
|
204
|
+
|
205
|
+
for link in links:
|
206
|
+
|
207
|
+
l_url = link.get('href')
|
208
|
+
|
209
|
+
else:
|
210
|
+
|
211
|
+
break
|
212
|
+
|
213
|
+
num += 1
|
214
|
+
|
215
|
+
else:
|
216
|
+
|
217
|
+
break
|
218
|
+
|
219
|
+
i += 1
|
220
|
+
|
95
221
|
get_href()
|
96
222
|
|
97
|
-
|
223
|
+
```
|
98
|
-
|
99
|
-
|
100
|
-
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
|
228
|
+
|
101
|
-
【機能2】
|
229
|
+
### 動作するソースコード【機能2】
|
230
|
+
|
231
|
+
```jupyter
|
232
|
+
|
233
|
+
import sys
|
234
|
+
|
235
|
+
import requests
|
236
|
+
|
237
|
+
import re
|
238
|
+
|
239
|
+
import urllib.request, urllib.error
|
240
|
+
|
241
|
+
from bs4 import BeautifulSoup
|
242
|
+
|
243
|
+
|
102
244
|
|
103
245
|
def get_soup(url):
|
104
246
|
|
247
|
+
"""URLのSoupを取得する"""
|
248
|
+
|
105
|
-
|
249
|
+
html = requests.get(url)
|
106
|
-
|
250
|
+
|
107
|
-
|
251
|
+
return BeautifulSoup(html.content, "html.parser")
|
108
252
|
|
109
253
|
|
110
254
|
|
111
255
|
def scraping_gh():
|
112
256
|
|
113
|
-
|
114
|
-
|
115
|
-
|
257
|
+
"""Software Design の情報を取得"""
|
258
|
+
|
116
|
-
|
259
|
+
soup = get_soup("https://www.judo-ch.jp/sekkotsuinsrch/13/13201/030637/")
|
117
|
-
|
118
|
-
|
260
|
+
|
261
|
+
|
262
|
+
|
119
|
-
|
263
|
+
# 整骨院の名称
|
120
|
-
|
264
|
+
|
121
|
-
|
265
|
+
res_p = soup.find("span", class_="name")
|
122
|
-
|
266
|
+
|
123
|
-
|
267
|
+
res = res_p.find(text=re.compile(""))
|
124
|
-
|
268
|
+
|
125
|
-
|
269
|
+
print(res.string)
|
126
|
-
|
270
|
+
|
127
|
-
|
271
|
+
# ホームページのURL
|
128
|
-
|
272
|
+
|
129
|
-
|
273
|
+
res_p = soup.find("p", class_="lnk_url")
|
130
|
-
|
274
|
+
|
131
|
-
|
275
|
+
res = res_p.find(text=re.compile(""))
|
132
|
-
|
276
|
+
|
133
|
-
|
277
|
+
print(res.string)
|
134
|
-
|
135
|
-
|
136
278
|
|
137
279
|
scraping_gh()
|
138
280
|
|
139
281
|
```
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
【機能1】【機能2】はそれぞれ動作するのですが、一緒にすると、エラーも表示されなくなってしまいました。
|
144
|
-
|
145
|
-
わかる方、教えていただけると幸いです。
|