takashi kono's blog

コーヒーとキーボードと共に何かを記録していくブログ

Python3 で Requests モジュール使い方の備忘録

Requests モジュールの備忘録

備忘録を書くことで、自分自身が使い方を忘れた時にそれを思い出す手助けとする。

Requests とは

Web アクセスののための Python モジュール?だと思います。
そもそも作られた理由が、urllib2 という Python 付属のモジュールがややこしすぎたからだとか。

Requests の公式ページにかかれているのは以下の通り。

Requests is an elegant and simple HTTP library for Python, built for human beings.

私の拙い訳ですと、

Requests は Python のためのエレガントでシンプルな HTTP ライブラリで、人間のために作られました。

うーん。変ですね。
translate.google.co.jp さんに翻訳していただきましょう。

Requestsは、人間のために構築された、Python用のエレガントでシンプルなHTTPライブラリです。

わかりやすい!

半端な翻訳よりよっぽどいいですね!テクノロジー。すごいです。

公式ページは以下からたどれます。
英語版

Requests: HTTP for Humans — Requests 2.18.4 documentation

日本語版

Requests: 人間のためのHTTP — requests-docs-ja 1.0.4 documentation

日本語版は、リリースバージョンが v1.0.4 と少し古い? 英語版だと v2.18.4 ですのでやはり古く感じてしまいますね。

更新履歴があるところを見つけました。良かったです。

Community Updates — Requests 2.18.4 documentation

インストール

$ pip install requests

で入るはずです。
入らなければ、公式を見ましょう。

Installation of Requests — Requests 2.18.4 documentation

Getting Started

ここにも書いてあるので、それでもいいと思います。

http://docs.python-requests.org/en/master/user/quickstart/#quickstart

やり方はそれぞれだと思いますが、やはり、インタプリタで試してからテキストファイルに落とすほうが確実だと思います。
今回はインタプリタで実行していきます。  

>>> import requests
>>> target_url = "https://github.com"
>>> r = requests.get(target_url)
>>> r.status_code
200

とりあえず、アクセスが出来ました。
この時拾ってきたレスポンスボディを表示させてみましょう。

>>> r.text
'\n\n\n\n\n\n<!DOCTYPE html>\n<html lang="en">\n  <head>\n    <meta charset="utf-8">\n  <link rel="dns-prefetch" href="https://assets-cdn.github.com">\n  <link rel="dns-prefetch" href="https://avatars0.githubusercontent.com">\n  <link rel="dns-prefetch" href="https://avatars1.githubusercontent.com">\n  <link rel="dns-prefetch" href="https://avatars2.githubusercontent.com">\n  <link rel="dns-prefetch" href="https://avatars3.githubusercontent.com">\n  <link rel="dns-prefetch" href="https://github-cloud.s3.amazonaws.com">\n  <link rel="dns-prefetch" href="https://user-images.githubusercontent.com/">\n\n\n\n 
...

ちゃんと取ってこれているようです。整形されていないので、見づらいですね。
そういう問のために、BeautifulSoup4 という素敵なものもあるんですが、それはまた別途使ってみようと思います。

ちなみにこの時の r が持つメソッド一覧

>>> [print(x) for x in dir(r)]
__attrs__
__bool__
__class__
__delattr__
__dict__
__dir__
__doc__
__enter__
__eq__
__exit__
__format__
__ge__
__getattribute__
__getstate__
__gt__
__hash__
__init__
__init_subclass__
__iter__
__le__
__lt__
__module__
__ne__
__new__
__nonzero__
__reduce__
__reduce_ex__
__repr__
__setattr__
__setstate__
__sizeof__
__str__
__subclasshook__
__weakref__
_content
_content_consumed
_next
apparent_encoding
close
connection
content
cookies
elapsed
encoding
headers
history
is_permanent_redirect
is_redirect
iter_content
iter_lines
json
links
next
ok
raise_for_status
raw
reason
request
status_code
text
url
[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
>>> 

この辺の詳しいことは、以下の リンク にも書いてあります。

Developer Interface — Requests 2.18.4 documentation

どんな値を拾ってデバッグしたいかによって、使いたいメソッド?が変わると思いますので、適宜ドキュメントを活用しましょう。
たとえば、ヘッダー情報を見てみたいときは、headers だったり、 request.headers を使えばわかります。

Requests は色々できて、ログインしてセッション維持して次のページへ遷移するとか、フォームに値を入れるとか、ユーザーエージェント情報を偽装?するとか。

そのあたりも順次追記していきたいと思います。


とりあえず、今日はここまで。(内容薄いな)
また、このページに追記します。