質問編集履歴

3

お礼追加

2018/08/10 12:39

投稿

Yukiya025
Yukiya025

スコア86

test CHANGED
File without changes
test CHANGED
@@ -283,3 +283,69 @@
283
283
  TypeError: 'unicode' object is not callable
284
284
 
285
285
  ```
286
+
287
+
288
+
289
+ # もう一度アドバイスをもらって完成
290
+
291
+ [umyu様](https://teratail.com/users/umyu) にスタックトレースの読み方まで教えていただいての完成です(*≧∀≦)
292
+
293
+ `html = r.text()`部分は括弧をとりましたが、それでも既視感のあるエラー (AttributeErrorだったかな?) がまた出て堂々巡りコロコロ ⌒((:з)⌒((ε:)⌒((:3に入った感があったので心が折れ、[案2: beautifulsoup4を使う](https://teratail.com/questions/140261#reply-212163) に切替え、`html = r.read()`に戻しましたorz
294
+
295
+
296
+
297
+ [https://news.google.com](https://news.google.com)をスクレイピング対象にしていましたが、urlにhtmlがまったく入っていなかったので、BeautifulSoup4のドキュメントを対象にしました。
298
+
299
+
300
+
301
+ ```python
302
+
303
+ # ファイル名: python_ex289_ex293.py
304
+
305
+ # -*-coding:utf-8-*-
306
+
307
+ # 手本: https://github.com/calthoff/self_taught/blob/master/python_ex289.py/
308
+
309
+ import urllib2
310
+
311
+ from bs4 import BeautifulSoup
312
+
313
+
314
+
315
+ class Scraper:
316
+
317
+ def __init__(self, site):
318
+
319
+ self.site = site
320
+
321
+
322
+
323
+ def scrape(self):
324
+
325
+ r = urllib2.urlopen(self.site) # urlopen関数を実行するとResponseオブジェクトが返される。
326
+
327
+ html = r.read()
328
+
329
+ parser = "html.parser"
330
+
331
+ sp = BeautifulSoup(html, parser)
332
+
333
+ for tag in sp.find_all("a"):
334
+
335
+ url = tag.get("href")
336
+
337
+ if url is None:
338
+
339
+ continue
340
+
341
+ if "html" in url:
342
+
343
+ print("\n" + url)
344
+
345
+
346
+
347
+ news = "https://www.crummy.com/software/BeautifulSoup/bs4/doc/"
348
+
349
+ Scraper(news).scrape()
350
+
351
+ ```

2

TypeError: 'unicode' object is not callable内容追記

2018/08/10 12:38

投稿

Yukiya025
Yukiya025

スコア86

test CHANGED
File without changes
test CHANGED
@@ -267,3 +267,19 @@
267
267
 
268
268
 
269
269
  しかしエラー「`TypeError: 'unicode' object is not callable`」で白旗 (´;ω;`)
270
+
271
+ ```
272
+
273
+ Traceback (most recent call last):
274
+
275
+ File "python_ex289_ex293.py", line 25, in <module>
276
+
277
+ Scraper(news).scrape()
278
+
279
+ File "python_ex289_ex293.py", line 13, in scrape
280
+
281
+ html = r.text()
282
+
283
+ TypeError: 'unicode' object is not callable
284
+
285
+ ```

1

新たなエラー追記

2018/08/09 02:03

投稿

Yukiya025
Yukiya025

スコア86

test CHANGED
File without changes
test CHANGED
@@ -185,3 +185,85 @@
185
185
  Scraper(news).scrape()
186
186
 
187
187
  ```
188
+
189
+
190
+
191
+ # 案1を採用
192
+
193
+ [umyuさんの回答](https://teratail.com/questions/140261#reply-212163)から案1を採用し、`from bs3 import BeautifulSoup3`を`from BeautifulSoup import BeautifulSoup`に変更しました。おかげさまで`ImportError: No module named bs3` のエラーはなくなりました(≧∀≦)
194
+
195
+
196
+
197
+ その他いろいろエラーがあったので、解決できる部分は解決 (多分?) したのですが、エラー 「`TypeError: 'unicode' object is not callable`」が解決できませんorz
198
+
199
+
200
+
201
+ **只今のコード**
202
+
203
+ ```python:web.py
204
+
205
+ # -*-coding:utf-8-*-
206
+
207
+ # https://github.com/calthoff/self_taught/blob/master/python_ex289.py/
208
+
209
+ from BeautifulSoup import BeautifulSoup
210
+
211
+ import requests
212
+
213
+ import urllib3
214
+
215
+ from HTMLParser import HTMLParser
216
+
217
+ class Scraper:
218
+
219
+ def __init__(self, site): # __init__メソッドはスクレイピング対象のURLを受け取る。
220
+
221
+ self.site = site
222
+
223
+
224
+
225
+ def scrape(self):
226
+
227
+ r = requests.get(self.site)
228
+
229
+ html = r.text()
230
+
231
+ parser = HTMLParser
232
+
233
+ sp = BeautifulSoup(html, parser)
234
+
235
+
236
+
237
+ for tag in sp.find_all("a"):
238
+
239
+ url = tag.get("href")
240
+
241
+ if url is None:
242
+
243
+ continue
244
+
245
+ if "html" in url:
246
+
247
+ print("\n" + url)
248
+
249
+
250
+
251
+ news = "https://news.google.com"
252
+
253
+ Scraper(news).scrape()
254
+
255
+ ```
256
+
257
+
258
+
259
+ ##エラーからの対応策概要
260
+
261
+ 1. urlopenが使えないようなのでrequests.getに変更
262
+
263
+ 2. `parser = "html.parser"`はPython2では使えないようなので`parser = HTMLParser`に変更
264
+
265
+ 3. `AttributeError: 'Response' object has no attribute 'read'`と出たので`html = r.read()`を`html = r.text()`に変更。
266
+
267
+
268
+
269
+ しかしエラー「`TypeError: 'unicode' object is not callable`」で白旗 (´;ω;`)