memo-2018_1125_1611

memo-2018_1125_1611

"""
複数のページをクロールするならSessionオブジェクトを使う。
r'https://.*'にリクエストを送る場合TCPコネクション確率時に処理される、
暗号化(TLS/SSLハンドシェイク)の負荷を軽減してあげられる。
HTTPヘッダー, Basic認証, Cookieも引き継げる。
HTTP Keep-Aliveと呼ばれる接続方式らしい。
"""

>>> s = requests.Session()
>>> s
<requests.sessions.Session object at 0x7f3e4497d0b8>
>>>
>>> pprint(dict(s.headers))
{'Accept': '*/*',
 'Accept-Encoding': 'gzip, deflate',
 'Connection': 'keep-alive',
 'User-Agent': 'python-requests/2.20.1'}
>>>
>>> s.headers.update({'User-Agent': 'my-crawler/1.0 (+foo@example.com)'})
>>> pprint(dict(s.headers))
{'Accept': '*/*',
 'Accept-Encoding': 'gzip, deflate',
 'Connection': 'keep-alive',
 'User-Agent': 'my-crawler/1.0 (+foo@example.com)'}
>>>
>>> r = s.get('http://teityura.wjg.jp/')
>>> r.headers
{'Date': 'Sun, 25 Nov 2018 07:12:59 GMT', 'Server': 'Apache/2.4.25 (Raspbian)', 'Link': '<http://teityura.wjg.jp/wp-json/>; rel="https://api.w.org/"', 'Vary': 'Accept-Encoding', 'Content-Encoding': 'gzip', 'Content-Length': '10614', 'Keep-Alive': 'timeout=5, max=100', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html; charset=UTF-8'}
>>>
>>> r = s.get('http://teityura.wjg.jp:8080/owncloud/')
>>> r.headers
{'Date': 'Sun, 25 Nov 2018 07:14:16 GMT', 'Server': 'Apache/2.4.25 (Raspbian)', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Cache-Control': 'no-cache, must-revalidate', 'Pragma': 'no-cache', 'Content-Security-Policy': "default-src 'none';manifest-src 'self';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'", 'Vary': 'Accept-Encoding', 'Content-Encoding': 'gzip', 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '1; mode=block', 'X-Robots-Tag': 'none', 'X-Frame-Options': 'SAMEORIGIN', 'X-Download-Options': 'noopen', 'X-Permitted-Cross-Domain-Policies': 'none', 'Content-Length': '2021', 'Keep-Alive': 'timeout=5, max=99', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html; charset=UTF-8'}

コメント

タイトルとURLをコピーしました