Webページからデータを取得するライブラリをインストールする。
> pip install lxml > pip install cssselect |
コマンドでインストール済みか確認する。
>pip list cssselect (1.0.1) lxml (3.8.0) >pip freeze lxml==3.8.0 cssselect==1.0.1 |
lxmlライブラリを用いて、Webページからaタグの情報を取得する。
import lxml.html html = tree.getroot() for a in html.cssselect( 'a' ): print (a.get( 'href' ), a.text) # 出力結果 # genindex.html 索引 # py-modindex.html モジュール # https://www.python.org/ Python # ~略~ |
Python3.6.1で確認した。
コメント