質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

457閲覧

[Python] http.cookiejar が正しく実装されているか

退会済みユーザー

退会済みユーザー

総合スコア0

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/03/06 15:51

編集2019/03/07 10:19

前提・実現したいこと

http.cookiejar モジュールで「HTTP クッキーの自動処理」を行っています。
レスポンスヘッダーを print出力 して、使用されたクッキーを確認したいです。

発生している問題

レスポンスヘッダーを print出力 しても、クッキーが存在しません。

レスポンスヘッダーを print出力

index:0 ('x-cloud-trace-context', '30ec2f92b7428495baf20a605e4fd483/12847133607480109894;o=0') index:1 ('pjsc-billing-credit-cost', '0.000243545') index:2 ('pjsc-billing-elapsedms', '5046') index:3 ('pjsc-billing-bytes', '143,001') index:4 ('pjsc-billing-total-credits-remaining', '0.043548346') index:5 ('pjsc-billing-daily-subscription-credits-remaining', '0.042548346') index:6 ('pjsc-billing-prepaid-credits-remaining', '0.001') index:7 ('local-address', '107.178.232.252') index:8 ('pjsc-backend-id', '1-zv9y') index:9 ('pjsc-content-status-code', '200') index:10 ('pjsc-content-name', 'login-yahoo-co-jp-config-login.html') index:11 ('pjsc-content-url', 'https://login.yahoo.co.jp/config/login') index:12 ('pjsc-content-page-exec-last-waited-on', 'waitInterval(1000) not yet met. still need to wait 2') index:13 ('pjsc-content-done-detail', '{"reason":"normal","statusCode":200}') index:14 ('pjsc-content-resource-aborted', '0') index:15 ('pjsc-content-resource-failed', '0') index:16 ('pjsc-content-resource-complete', '16') index:17 ('pjsc-content-resource-active', '0') index:18 ('pjsc-content-resource-late', '0') index:19 ('pjsc-content-event-phase', '"load"') index:20 ('content-disposition', 'filename="login-yahoo-co-jp-config-login.html"') index:21 ('content-type', 'application/json; charset=utf-8') index:22 ('vary', 'origin,accept-encoding') index:23 ('cache-control', 'no-cache') index:24 ('content-length', '159694') index:25 ('accept-ranges', 'bytes') index:26 ('Date', 'Wed, 06 Mar 2019 15:04:53 GMT') index:27 ('Via', '1.1 google') index:28 ('Alt-Svc', 'clear') index:29 ('Connection', 'close')

該当のソースコード

Python

1import http.cookiejar 2import json 3import re 4import urllib.parse 5import urllib.request 6from urllib.request import HTTPCookieProcessor 7 8def getCookie_afterLogin(request): 9 10 # HTTP通信時にCookie処理を有効化します 11 cj = http.cookiejar.CookieJar() 12 opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) 13 14 15 # Yahoo! トップページ URL 16 url = "https://www.yahoo.co.jp/" 17 opener.addheaders = [ 18 ('User-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0') # クッキーB取得に必須 19 ] 20 with opener.open(url) as res: 21 header = res.getheaders() # list 22 23 cookie_B = '初期値' 24 for tuple in header: 25 if tuple[1][0:2] == 'B=': 26 cookie_B = tuple[1] 27 break 28 print('cookie_B:', cookie_B) 29 30 31 # Yahoo! ログイン URL 32 url = "https://login.yahoo.co.jp/config/login" 33 opener.addheaders = [ 34 ('User-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0') # クッキーB取得に必須 35 ] 36 payload = {'url':url, 'renderType':'HTML', 'outputAsJson':'true'} 37 payload = json.dumps(payload) #JSONパース 38 payload = urllib.parse.quote(payload,safe = '') # URIエンコード 39 key = '**-#####-*****-#####-*****-#####' 40 url = "https://phantomjscloud.com/api/browser/v2/"+ key+"/?request=" + payload 41 with opener.open(url) as res: 42 header = res.getheaders() # list 43 json_body = res.read().decode('utf-8') # str 44 for index, value in enumerate(header): 45 print('index:'+ str(index), value) 46 47 pattern1 = r'<form method=\"post\" action=\"/config/login\" name=\"login_form\">\n[\s\S]*?\n\n<div class=\"inputArea\" id=\"inputArea\">' 48 pattern2 = r'<form method="post" action="/config/login" name="login_form">\n([\s\S]*?)\n\n<div class="inputArea" id="inputArea">' 49 hiddenPart = re.search(pattern1, json_body) 50 hiddenPart = hiddenPart.group() 51 hiddenPart = re.sub(r'\n', r'\n', hiddenPart) 52 hiddenPart = re.sub(r'\', r'', hiddenPart) 53 hiddenPart = re.sub(pattern2, r'\1', hiddenPart) 54 return hiddenPart

問題の発生した環境

Google Cloud Platform

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問