亀の甲羅2

今日もまた朝とく起きて励まなん窓に明るきありあけの月

Python で Webスクレイピング (2)

そもそもpythonってどうやって実行するの?って所から。

目次

本文

1. VS Code

python」という拡張機能をインストールすれば、あとは普通にコーディングできる。実行も、VSCode内のターミナル上で実行することも可能だし、普通の開発環境のようにステップ実行しデバッグしたりもできる。それなりに大きいコードを書くときには便利だと思う。Git連携も簡単だし。

2. jupyter notebook

2.1 jupyter notebook のインストール

pipでインストールする必要がある。

PS > python -m pip install jupyter
Collecting jupyter
  Using cached jupyter-1.0.0-py2.py3-none-any.whl (2.7 kB)

 (中略)

Using legacy 'setup.py install' for pandocfilters, since package 'wheel' is not installed.
Installing collected packages: traitlets, six, pywin32, pyrsistent, attrs, wcwidth, tornado, pyzmq, python-dateutil, pyparsing, parso, nest-asyncio, jupyter-core, jsonschema, ipython-genutils, entrypoints, webencodings, pygments, pycparser, prompt-toolkit, pickleshare, packaging, nbformat, matplotlib-inline, MarkupSafe, jupyter-client, jedi, decorator, colorama, backcall, testpath, pywinpty, pandocfilters, nbclient, mistune, jupyterlab-pygments, jinja2, ipython, defusedxml, debugpy, cffi, bleach, terminado, Send2Trash, prometheus-client, nbconvert, ipykernel, argon2-cffi, notebook, widgetsnbextension, qtpy, jupyterlab-widgets, qtconsole, jupyter-console, ipywidgets, jupyter
    Running setup.py install for pandocfilters ... done
Successfully installed MarkupSafe-2.0.1 Send2Trash-1.8.0 argon2-cffi-21.1.0 attrs-21.2.0 backcall-0.2.0 bleach-4.1.0 cffi-1.14.6 colorama-0.4.4 debugpy-1.4.1 decorator-5.0.9 defusedxml-0.7.1 entrypoints-0.3 ipykernel-6.3.1 ipython-7.27.0 ipython-genutils-0.2.0 ipywidgets-7.6.4 jedi-0.18.0 jinja2-3.0.1 jsonschema-3.2.0 jupyter-1.0.0 jupyter-client-7.0.2 jupyter-console-6.4.0 jupyter-core-4.7.1 jupyterlab-pygments-0.1.2 jupyterlab-widgets-1.0.1 matplotlib-inline-0.1.2 mistune-0.8.4 nbclient-0.5.4 nbconvert-6.1.0 nbformat-5.1.3 nest-asyncio-1.5.1 notebook-6.4.3 packaging-21.0 pandocfilters-1.4.3 parso-0.8.2 pickleshare-0.7.5 prometheus-client-0.11.0 prompt-toolkit-3.0.20 pycparser-2.20 pygments-2.10.0 pyparsing-2.4.7 pyrsistent-0.18.0 python-dateutil-2.8.2 pywin32-301 pywinpty-1.1.4 pyzmq-22.2.1 qtconsole-5.1.1 qtpy-1.11.0 six-1.16.0 terminado-0.11.1 testpath-0.5.0 tornado-6.1 traitlets-5.1.0 wcwidth-0.2.5 webencodings-0.5.1 widgetsnbextension-3.5.1
PS >

2.2 jupyter notebook の起動

ターミナルなどから、以下を実行。ちょっとしたコードの実行には楽。

PS > jupyter notebook

ブラウザが起動しlocalhostのページが起動する。

f:id:takobose_ht:20210906212657p:plain

詳しい使い方は別稿とする。

3. 対話的に

ターミナル上でpythonと入力すると、pythonモードになる。プロンプトが「>>>」となるのでわかりやすい。

>python
Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("aaaa")
aaaa
>>> a = 1
>>> print(a)
1
>>>

4. pyファイルの実行

ターミナルでpython python-file.pyでファイル内のコードを実行できる。

>type hello.py
print("Hello World!!");

>python hello.py
Hello World!!

バージョン情報

・Windows10
・Python 3.9.7