pythonを使ってスクレイピングをやりたいのですがbeautifulsoup4はインストールできたのですが
import urllib.request
をやろうとすると
ImportError: No module named request
が出てきてしまって実行できません。
何が原因なのでしょうか?
また、https://github.com/calthoff/self_taught/blob/master/python_ex293.py/
ここにあるものをそのままコピペで対応しようとしたのですが
import urllib.request
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named request
from bs4 import BeautifulSoup
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named bs4
class Scraper:
... def init(self, site):
... self.site = site
...
def scrape(self):
File "<stdin>", line 1
def scrape(self):
^
IndentationError: unexpected indent
r = urllib.request\
File "<stdin>", line 1
r = urllib.request
^
IndentationError: unexpected indent
.urlopen(self.site)
File "<stdin>", line 1
.urlopen(self.site)
^
IndentationError: unexpected indent
html = r.read()
File "<stdin>", line 1
html = r.read()
^
IndentationError: unexpected indent
parser = "html.parser"
File "<stdin>", line 1
parser = "html.parser"
^
IndentationError: unexpected indent
sp = BeautifulSoup(html,
File "<stdin>", line 1
sp = BeautifulSoup(html,
^
IndentationError: unexpected indent
parser)
File "<stdin>", line 1
parser)
^
IndentationError: unexpected indent
for tag in sp.find_all("a"):
File "<stdin>", line 1
for tag in sp.find_all("a"):
^
IndentationError: unexpected indent
url = tag.get("href")
File "<stdin>", line 1
url = tag.get("href")
^
IndentationError: unexpected indent
if url is None:
File "<stdin>", line 1
if url is None:
^
IndentationError: unexpected indent
continue
File "<stdin>", line 1
continue
^
IndentationError: unexpected indent
if "html" in url:
File "<stdin>", line 1
if "html" in url:
^
IndentationError: unexpected indent
print("\n" + url)
File "<stdin>", line 1
print("\n" + url)
^
IndentationError: unexpected indent
news = "https://news.google.com/"
Scraper(news).scrape()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Scraper instance has no attribute 'scrape'
結果は以上のようになりました。
発生している問題・エラーメッセージ
エラーメッセージ ```ImportError: No module named request ### 該当のソースコード >>> import urllib.request Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named request ### 補足情報(FW/ツールのバージョンなど) python3
回答2件
あなたの回答
tips
プレビュー