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

質問編集履歴

3

お礼追加

2018/08/10 12:39

投稿

Yukiya025
Yukiya025

スコア86

title CHANGED
File without changes
body CHANGED
@@ -140,4 +140,37 @@
140
140
  File "python_ex289_ex293.py", line 13, in scrape
141
141
  html = r.text()
142
142
  TypeError: 'unicode' object is not callable
143
+ ```
144
+
145
+ # もう一度アドバイスをもらって完成
146
+ [umyu様](https://teratail.com/users/umyu) にスタックトレースの読み方まで教えていただいての完成です(*≧∀≦)
147
+ `html = r.text()`部分は括弧をとりましたが、それでも既視感のあるエラー (AttributeErrorだったかな?) がまた出て堂々巡りコロコロ ⌒((:з)⌒((ε:)⌒((:3に入った感があったので心が折れ、[案2: beautifulsoup4を使う](https://teratail.com/questions/140261#reply-212163) に切替え、`html = r.read()`に戻しましたorz
148
+
149
+ [https://news.google.com](https://news.google.com)をスクレイピング対象にしていましたが、urlにhtmlがまったく入っていなかったので、BeautifulSoup4のドキュメントを対象にしました。
150
+
151
+ ```python
152
+ # ファイル名: python_ex289_ex293.py
153
+ # -*-coding:utf-8-*-
154
+ # 手本: https://github.com/calthoff/self_taught/blob/master/python_ex289.py/
155
+ import urllib2
156
+ from bs4 import BeautifulSoup
157
+
158
+ class Scraper:
159
+ def __init__(self, site):
160
+ self.site = site
161
+
162
+ def scrape(self):
163
+ r = urllib2.urlopen(self.site) # urlopen関数を実行するとResponseオブジェクトが返される。
164
+ html = r.read()
165
+ parser = "html.parser"
166
+ sp = BeautifulSoup(html, parser)
167
+ for tag in sp.find_all("a"):
168
+ url = tag.get("href")
169
+ if url is None:
170
+ continue
171
+ if "html" in url:
172
+ print("\n" + url)
173
+
174
+ news = "https://www.crummy.com/software/BeautifulSoup/bs4/doc/"
175
+ Scraper(news).scrape()
143
176
  ```

2

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

2018/08/10 12:38

投稿

Yukiya025
Yukiya025

スコア86

title CHANGED
File without changes
body CHANGED
@@ -132,4 +132,12 @@
132
132
  2. `parser = "html.parser"`はPython2では使えないようなので`parser = HTMLParser`に変更
133
133
  3. `AttributeError: 'Response' object has no attribute 'read'`と出たので`html = r.read()`を`html = r.text()`に変更。
134
134
 
135
- しかしエラー「`TypeError: 'unicode' object is not callable`」で白旗 (´;ω;`)
135
+ しかしエラー「`TypeError: 'unicode' object is not callable`」で白旗 (´;ω;`)
136
+ ```
137
+ Traceback (most recent call last):
138
+ File "python_ex289_ex293.py", line 25, in <module>
139
+ Scraper(news).scrape()
140
+ File "python_ex289_ex293.py", line 13, in scrape
141
+ html = r.text()
142
+ TypeError: 'unicode' object is not callable
143
+ ```

1

新たなエラー追記

2018/08/09 02:03

投稿

Yukiya025
Yukiya025

スコア86

title CHANGED
File without changes
body CHANGED
@@ -91,4 +91,45 @@
91
91
 
92
92
  news = "https://news.google.com"
93
93
  Scraper(news).scrape()
94
- ```
94
+ ```
95
+
96
+ # 案1を採用
97
+ [umyuさんの回答](https://teratail.com/questions/140261#reply-212163)から案1を採用し、`from bs3 import BeautifulSoup3`を`from BeautifulSoup import BeautifulSoup`に変更しました。おかげさまで`ImportError: No module named bs3` のエラーはなくなりました(≧∀≦)
98
+
99
+ その他いろいろエラーがあったので、解決できる部分は解決 (多分?) したのですが、エラー 「`TypeError: 'unicode' object is not callable`」が解決できませんorz
100
+
101
+ **只今のコード**
102
+ ```python:web.py
103
+ # -*-coding:utf-8-*-
104
+ # https://github.com/calthoff/self_taught/blob/master/python_ex289.py/
105
+ from BeautifulSoup import BeautifulSoup
106
+ import requests
107
+ import urllib3
108
+ from HTMLParser import HTMLParser
109
+ class Scraper:
110
+ def __init__(self, site): # __init__メソッドはスクレイピング対象のURLを受け取る。
111
+ self.site = site
112
+
113
+ def scrape(self):
114
+ r = requests.get(self.site)
115
+ html = r.text()
116
+ parser = HTMLParser
117
+ sp = BeautifulSoup(html, parser)
118
+
119
+ for tag in sp.find_all("a"):
120
+ url = tag.get("href")
121
+ if url is None:
122
+ continue
123
+ if "html" in url:
124
+ print("\n" + url)
125
+
126
+ news = "https://news.google.com"
127
+ Scraper(news).scrape()
128
+ ```
129
+
130
+ ##エラーからの対応策概要
131
+ 1. urlopenが使えないようなのでrequests.getに変更
132
+ 2. `parser = "html.parser"`はPython2では使えないようなので`parser = HTMLParser`に変更
133
+ 3. `AttributeError: 'Response' object has no attribute 'read'`と出たので`html = r.read()`を`html = r.text()`に変更。
134
+
135
+ しかしエラー「`TypeError: 'unicode' object is not callable`」で白旗 (´;ω;`)