用于表单发布的Python请求会话似乎不会持久

时间:2018-06-06 04:13:02

标签: python html session python-requests persistence

我试图(1)登录带有请求的网站,然后(2)更新登录限制子页面上的六个文本字段的值。我的代码似乎成功登录。状态代码为200,print(login.text)返回的输出仅在成功登录后显示登录页面的HTML。

但是,一旦我尝试将数据发布到下一页的六个字段(diary_url),我的请求会话似乎仍未保持登录状态。

有谁知道导致我的问题的原因是什么?下面的相关HTML和Python。

登录页面的HTML

<div class="member-login">
    
    <h2>Member Login</h2>
    <ul class="list-unstyled">
      <li>
      </li>
      <li class="username-field">
          <label>Email Address:</label>
          <input autocapitalize="none" class="text" id="username" name="username" placeholder="Email Address" size="12" type="text" value="" />
      </li>
      <li class="password-field">
        <label>Password:</label>
        <input class="text" id="password" name="password" placeholder="Password" size="12" type="password" value="" />
      </li>
      <li class="remember-field">
        <input checked="checked" class="checkbox" id="remember_me" name="remember_me" type="checkbox" value="1" />
        <label for="remember_me" >Remember me next time</label>
      </li>
      <li class="submit" >
        <input type="submit" value="Log In" />
      </li>
      <li class="or or-static-page" >
        <span>or</span>
        <div class="hr"></div>
      </li>
      <li>
        <a href="#" class="facebook-login-css bttn btn-primary" id="facebook-login-css">Log in with Facebook</a>
      </li>
    </ul>
  </div>

第二页的HTML,我想通过帖子提交新数据

<div class="col-1">

        <h4>Meal Names:</h4>

        <ol class="fieldset">

            
              <li class="field">
                <input class="text" id="meal_names_0_description" maxlength="30" name="meal_names[0][description]" size="30" type="text" value="A. 35P 3V 0F 20C" />
              </li>
            
              <li class="field">
                <input class="text" id="meal_names_1_description" maxlength="30" name="meal_names[1][description]" size="30" type="text" value="B. 35P 3V 0F 20C" />
              </li>
            
              <li class="field">
                <input class="text" id="meal_names_2_description" maxlength="30" name="meal_names[2][description]" size="30" type="text" value="C. 35P 3V 10F 20C" />
              </li>
            
              <li class="field">
                <input class="text" id="meal_names_3_description" maxlength="30" name="meal_names[3][description]" size="30" type="text" value="D. 35P 3V 10F 35C" />
              </li>
            
              <li class="field">
                <input class="text" id="meal_names_4_description" maxlength="30" name="meal_names[4][description]" size="30" type="text" value="E. 35P 3V 15F 55C" />
              </li>
            
              <li class="field">
                <input class="text" id="meal_names_5_description" maxlength="30" name="meal_names[5][description]" size="30" type="text" value="WO. 35P 0V 0F 65C" />
              </li>

        </ol>

      </div>

自定义Python代码以登录网站,然后提交新字段名

import secrets, requests

meals={
    0:'Leaf',
    1:'Crackor',
    2:'Stinky',
    3:'Grape',
    4:'Appol',
    5:'Bellay'
}

mealnames={
    'meal_names_0_description': meals[0],
    'meal_names_1_description': meals[1],
    'meal_names_2_description': meals[2],
    'meal_names_3_description': meals[3],
    'meal_names_4_description': meals[4],
    'meal_names_5_description': meals[5]
}

payload={
    'username': secrets.user,
    'password': secrets.pw
}

with requests.Session() as s:
    login_url="https://www.myfitnesspal.com/account/login"
    diary_url='https://www.myfitnesspal.com/account/diary_settings'

    login = s.post(login_url, data=payload)
    if(login.status_code!=200):
        print("BAD")

    go_to_diary = s.get(diary_url)
    diary_page = s.post(diary_url, data=mealnames)

0 个答案:

没有答案