使用python登录到网站 [英] Login to a website using python

查看:135
本文介绍了使用python登录到网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python登录此页面.这是我的代码

I am trying to login to this page using Python.Here is my code

  from urllib2 import urlopen                        
  from bs4 import BeautifulSoup
  import requests
  import sys


  URL= 'http://coe2.annauniv.edu/result/index.php'
  soup = BeautifulSoup(urlopen(URL))

  for hit in soup.findAll(attrs={'class' : 's2'}):
  print hit.contents[0].strip()


  RegisterNumber = raw_input("enter the register number")
  DateofBirth = raw_input("enter the date of birth [DD-MM-YYYY]")
  login_input = raw_input("enter the what is()?")

 def main():
    # Start a session so we can have persistant cookies
     session = requests.session()

# This is the form data that the page sends when logging in
login_data = {
    'register_no':'RegisterNumber',
    'dob':'DateofBirth',
    'security_code_student' :'login_input',
    'gos': 'Login',
}

# Authenticate
r = session.post(URL, data=login_data)

# Try accessing a page that requires you to be logged in
r = session.get('http://coe2.annauniv.edu/result/students_corner.php')

 if __name__ == '__main__':
      main() 

我尝试使用请求.但是上面的代码无法访问需要登录的页面.

i tried using requests .but the above code can't accessed the page that requires to be login.

我在做什么错了?

推荐答案

您错误地使用了字符串值而不是login_data中的预期变量.

You are incorrectly using string values instead of the intended variables inside login_data.

from urllib2 import urlopen                        
from bs4 import BeautifulSoup
import requests
import sys

URL= 'http://coe2.annauniv.edu/result/index.php'
soup = BeautifulSoup(urlopen(URL))
#print soup.prettify()

for hit in soup.findAll(attrs={'class' : 's2'}):
    print hit.contents[0].strip()

RegisterNumber = raw_input("Enter the registration number: ")
DateofBirth = raw_input("Enter the date of birth [DD-MM-YYYY]: ")
login_input = raw_input("Enter the what is()? ")

def main():
    # Start a session so we can have persistant cookies

    # Session() >> http://docs.python-requests.org/en/latest/api/#request-sessions
    session = requests.Session() 

    # This is the form data that the page sends when logging in

    # You are wrongly using string values instead of the intended variables here that is RegisterNumber and not 'RegisterNumber'
    login_data = {
    'register_no': RegisterNumber,
    'dob': DateofBirth,
    'security_code_student': login_input,
    'gos': 'Login',
    }
    print login_data

    # Authenticate
    r = session.post(URL, data = login_data)
    # Try accessing a page that requires you to be logged in
    r = session.get('http://coe2.annauniv.edu/result/students_corner.php')
    print r

if __name__ == '__main__':
    main()

P.S .:如果您正在寻找其他东西,请找回,但要张贴您想要的东西和您精心制作的东西!

P.S.: Get back if you are looking for something else, but post what you wished and what you got elaborately!

这篇关于使用python登录到网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆