LoginSignup
TakumiTheEarth
@TakumiTheEarth (Takumi)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

Python, urllibを使ったログインがうまくいかない

解決したいこと

Python、urllibを使いソースコード内のサイトにログインし、ログイン後のhtmlを取得したいのですがどうしてもログインができず、ログイン画面のhtmlしか取得できません。
自分で思いつく限りの事は行ったのですが...どこに原因があるでしょうか?

該当するソースコード

import urllib.request
import json

URL = 'https://eowpf.alc.co.jp/login'

data = {
    'username': 'emailaddress',
    'password': 'password',
    'login-form-type': 'pwd',
    'checkbox': '1',
    'login': 'ログイン'
    }

headers = {'Content-Type': 'application/json'}

opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor())
req = opener.open(urllib.request.Request(URL,json.dumps(data).encode(),headers))
res = req.read().decode()
print(res)

サイト側のhtml (細かいところは省略しています)

<form id="fmLogin" name="form1" method="post" onsubmit="javascript:return valCheck()" action="/login">
	<input id="username" name="username" type="text" size="25" class="fInput">
	<input id="password" name="password" type="password" size="25" class="fInput" autocomplete="off">
    <input type="checkbox" name="checkbox" value="1">
    <input type="hidden" name="login-form-type" value="pwd">
    <input id="submit_button" name="login" type="submit" value="ログイン" class="submitbutt">
</form>
0

1Answer

MIMEタイプがapplication/x-www-form-urlencodedで、formパラメタでpostするものと思います。

2

Comments

  1. @TakumiTheEarth

    Questioner

    解決しました!一人では辿り着けませんでした、ありがとうございます。

  2. @TakumiTheEarth

    Questioner

    すみません、恥ずかしながら勘違いでした...。
    やはりログイン前のhtmlが取得されてしまいます、他に考えられる原因はあるでしょうか?

  3. application/x-www-form-urlencodedでPOSTすることは間違いないと思います。
    変更後のコードを見せてください。

  4. @TakumiTheEarth

    Questioner

    ありがとうございます、コードこちらです。

    import urllib.request
    import json

    URL = 'https://eowpf.alc.co.jp/login'

    data = {
    'username': 'xxx',
    'password': 'xxx',
    'login-form-type': 'pwd',
    'checkbox': '1',
    'login': 'ログイン'
    }
    headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
    }
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor())
    req = opener.open(urllib.request.Request(URL,json.dumps(data).encode(),headers))
    res = req.read().decode()
    print(res)

  5. dataはjsonでなくformパラメタで指定する必要があります。


    ↓こんな感じかな?

    res= opener.open(urllib.request.Request(URL, urllib.parse.urlencode(data), headers))
    print(res)
    
  6. @TakumiTheEarth

    Questioner

    今度こそできました!お手数おかけしました、ありがとうございます。

    import urllib.request
    import urllib.parse
    
    URL = 'https://eowpf.alc.co.jp/login'
    
    data = {
        'username': 'xxx',
        'password': 'xxx',
        'login-form-type': 'pwd',
        'checkbox': '1',
        'login': 'ログイン'
        }
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
        }
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor())
    req = opener.open(urllib.request.Request(URL,urllib.parse.urlencode(data).encode(),headers))
    res = req.read().decode()
    print(res)
    
  7. ✌️

    解決ならば、当Q&Aをクローズしてくださいね。

Your answer might help someone💌