いきなり request をインポートしようとすると「request モジュールがありません」とのことでエラーが出てしまいます。
>>> import requests Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'requests'
なので一旦 Python は exit() します。そして pip3 で request をインストール。
>>> exit() % pip3 install requests
すると下記の様に諸々のダウンロードが実行され一瞬で入力待ち状態に戻ります。
% pip3 install requests Collecting requests Downloading requests-2.24.0-py2.py3-none-any.whl (61 kB) |████████████████████████████████| 61 kB 692 kB/s Collecting certifi>=2017.4.17 Downloading certifi-2020.6.20-py2.py3-none-any.whl (156 kB) |████████████████████████████████| 156 kB 6.7 MB/s Collecting idna<3,>=2.5 Downloading idna-2.10-py2.py3-none-any.whl (58 kB) |████████████████████████████████| 58 kB 7.5 MB/s Collecting chardet<4,>=3.0.2 Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB) |████████████████████████████████| 133 kB 16.4 MB/s Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 Downloading urllib3-1.25.10-py2.py3-none-any.whl (127 kB) |████████████████████████████████| 127 kB 20.1 MB/s Installing collected packages: certifi, idna, chardet, urllib3, requests Successfully installed certifi-2020.6.20 chardet-3.0.4 idna-2.10 requests-2.24.0 urllib3-1.25.10 %
入力待ちになったら Python3 を起動。
% python3
そしてそのまま request をインポートしてみます。
>>> import requests >>>
今回はエラーが出ず入力待ち状態になりました。試しにそのまま Yahoo! のソースコードを表示してみます。
>>> import requests >>> r = requests.get('http://yahoo.com/') >>> r.text '<!DOCTYPE html>\n<html id="atomic" lang="en-US" class="atomic l-out Pos-r https fp fp-v2 fp-default mini-uh-on uh-topbar-on ltr desktop Desktop bkt201">\n<head>\n <meta http-equiv="X-UA-Compatible" content="IE=edge">\n \n <title>Yahoo</title><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="//s.yimg.com"><link rel="preconnect" href="//s.yimg.com">
問題なさそうですね。これで request のインストールは完了です。