質問編集履歴
3
加筆
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,6 +10,186 @@
|
|
10
10
|
|
11
11
|
何が原因なのでしょうか?
|
12
12
|
|
13
|
+
|
14
|
+
|
15
|
+
また、https://github.com/calthoff/self_taught/blob/master/python_ex293.py/
|
16
|
+
|
17
|
+
ここにあるものをそのままコピペで対応しようとしたのですが
|
18
|
+
|
19
|
+
>>> import urllib.request
|
20
|
+
|
21
|
+
Traceback (most recent call last):
|
22
|
+
|
23
|
+
File "<stdin>", line 1, in <module>
|
24
|
+
|
25
|
+
ImportError: No module named request
|
26
|
+
|
27
|
+
>>> from bs4 import BeautifulSoup
|
28
|
+
|
29
|
+
Traceback (most recent call last):
|
30
|
+
|
31
|
+
File "<stdin>", line 1, in <module>
|
32
|
+
|
33
|
+
ImportError: No module named bs4
|
34
|
+
|
35
|
+
>>>
|
36
|
+
|
37
|
+
>>>
|
38
|
+
|
39
|
+
>>> class Scraper:
|
40
|
+
|
41
|
+
... def __init__(self, site):
|
42
|
+
|
43
|
+
... self.site = site
|
44
|
+
|
45
|
+
...
|
46
|
+
|
47
|
+
>>> def scrape(self):
|
48
|
+
|
49
|
+
File "<stdin>", line 1
|
50
|
+
|
51
|
+
def scrape(self):
|
52
|
+
|
53
|
+
^
|
54
|
+
|
55
|
+
IndentationError: unexpected indent
|
56
|
+
|
57
|
+
>>> r = urllib.request\
|
58
|
+
|
59
|
+
File "<stdin>", line 1
|
60
|
+
|
61
|
+
r = urllib.request\
|
62
|
+
|
63
|
+
^
|
64
|
+
|
65
|
+
IndentationError: unexpected indent
|
66
|
+
|
67
|
+
>>> .urlopen(self.site)
|
68
|
+
|
69
|
+
File "<stdin>", line 1
|
70
|
+
|
71
|
+
.urlopen(self.site)
|
72
|
+
|
73
|
+
^
|
74
|
+
|
75
|
+
IndentationError: unexpected indent
|
76
|
+
|
77
|
+
>>> html = r.read()
|
78
|
+
|
79
|
+
File "<stdin>", line 1
|
80
|
+
|
81
|
+
html = r.read()
|
82
|
+
|
83
|
+
^
|
84
|
+
|
85
|
+
IndentationError: unexpected indent
|
86
|
+
|
87
|
+
>>> parser = "html.parser"
|
88
|
+
|
89
|
+
File "<stdin>", line 1
|
90
|
+
|
91
|
+
parser = "html.parser"
|
92
|
+
|
93
|
+
^
|
94
|
+
|
95
|
+
IndentationError: unexpected indent
|
96
|
+
|
97
|
+
>>> sp = BeautifulSoup(html,
|
98
|
+
|
99
|
+
File "<stdin>", line 1
|
100
|
+
|
101
|
+
sp = BeautifulSoup(html,
|
102
|
+
|
103
|
+
^
|
104
|
+
|
105
|
+
IndentationError: unexpected indent
|
106
|
+
|
107
|
+
>>> parser)
|
108
|
+
|
109
|
+
File "<stdin>", line 1
|
110
|
+
|
111
|
+
parser)
|
112
|
+
|
113
|
+
^
|
114
|
+
|
115
|
+
IndentationError: unexpected indent
|
116
|
+
|
117
|
+
>>> for tag in sp.find_all("a"):
|
118
|
+
|
119
|
+
File "<stdin>", line 1
|
120
|
+
|
121
|
+
for tag in sp.find_all("a"):
|
122
|
+
|
123
|
+
^
|
124
|
+
|
125
|
+
IndentationError: unexpected indent
|
126
|
+
|
127
|
+
>>> url = tag.get("href")
|
128
|
+
|
129
|
+
File "<stdin>", line 1
|
130
|
+
|
131
|
+
url = tag.get("href")
|
132
|
+
|
133
|
+
^
|
134
|
+
|
135
|
+
IndentationError: unexpected indent
|
136
|
+
|
137
|
+
>>> if url is None:
|
138
|
+
|
139
|
+
File "<stdin>", line 1
|
140
|
+
|
141
|
+
if url is None:
|
142
|
+
|
143
|
+
^
|
144
|
+
|
145
|
+
IndentationError: unexpected indent
|
146
|
+
|
147
|
+
>>> continue
|
148
|
+
|
149
|
+
File "<stdin>", line 1
|
150
|
+
|
151
|
+
continue
|
152
|
+
|
153
|
+
^
|
154
|
+
|
155
|
+
IndentationError: unexpected indent
|
156
|
+
|
157
|
+
>>> if "html" in url:
|
158
|
+
|
159
|
+
File "<stdin>", line 1
|
160
|
+
|
161
|
+
if "html" in url:
|
162
|
+
|
163
|
+
^
|
164
|
+
|
165
|
+
IndentationError: unexpected indent
|
166
|
+
|
167
|
+
>>> print("\n" + url)
|
168
|
+
|
169
|
+
File "<stdin>", line 1
|
170
|
+
|
171
|
+
print("\n" + url)
|
172
|
+
|
173
|
+
^
|
174
|
+
|
175
|
+
IndentationError: unexpected indent
|
176
|
+
|
177
|
+
>>>
|
178
|
+
|
179
|
+
>>> news = "https://news.google.com/"
|
180
|
+
|
181
|
+
>>> Scraper(news).scrape()
|
182
|
+
|
183
|
+
Traceback (most recent call last):
|
184
|
+
|
185
|
+
File "<stdin>", line 1, in <module>
|
186
|
+
|
187
|
+
AttributeError: Scraper instance has no attribute 'scrape'
|
188
|
+
|
189
|
+
>>>
|
190
|
+
|
191
|
+
結果は以上のようになりました。
|
192
|
+
|
13
193
|
### 発生している問題・エラーメッセージ
|
14
194
|
|
15
195
|
|
2
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -34,17 +34,7 @@
|
|
34
34
|
|
35
35
|
ImportError: No module named request
|
36
36
|
|
37
|
-
ソースコード
|
38
37
|
|
39
|
-
```
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
### 試したこと
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
ここに問題に対して試したことを記載してください。
|
48
38
|
|
49
39
|
|
50
40
|
|
1
内容の追加
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
python
|
1
|
+
pythonでスクレイピングする際、import urllib.requestが実行できません。
|
test
CHANGED
@@ -1,9 +1,3 @@
|
|
1
|
-
### 前提・実現したいこと
|
2
|
-
|
3
|
-
webスクレイピング
|
4
|
-
|
5
|
-
ここに質問の内容を詳しく書いてください。
|
6
|
-
|
7
1
|
pythonを使ってスクレイピングをやりたいのですがbeautifulsoup4はインストールできたのですが
|
8
2
|
|
9
3
|
import urllib.request
|
@@ -32,7 +26,13 @@
|
|
32
26
|
|
33
27
|
|
34
28
|
|
35
|
-
|
29
|
+
>>> import urllib.request
|
30
|
+
|
31
|
+
Traceback (most recent call last):
|
32
|
+
|
33
|
+
File "<stdin>", line 1, in <module>
|
34
|
+
|
35
|
+
ImportError: No module named request
|
36
36
|
|
37
37
|
ソースコード
|
38
38
|
|
@@ -52,4 +52,4 @@
|
|
52
52
|
|
53
53
|
|
54
54
|
|
55
|
-
|
55
|
+
python3
|