自动登录思路及其示例

自动登录GitHub

  1. 用reques请求获取响应(respones)
  2. 用BeautifulSoup将响应转化为对象
  3. 视情况而做出不同的操作,获取cookies,用
    session = requsets.session()

    获取网页信息等

  4. 用form表单发送信息
import requests
from bs4 import BeautifulSoup

r1 = requests.get("https://github.com/join")
soup1 = BeautifulSoup(r1.text,features="lxml")
tag = soup1.find(name="input",attrs={"name":"authenticity_token"})
authenticity_token = tag.get("value")

c1 = r1.cookies.get_dict()
r1.close()  #第一次获取cookies

form_data = {
    "utf8" : "",
    "authenticity_token" : authenticity_token,
    "commit" : "Sign in",
    "login" : "1115864412@qq.com",
    "password" : "houyu139322"
}        #携带authenticity_token和用户名密码等信息,发送用户验证

r2 = requests.get("https://github.com/join",data=form_data,cookies=c1)
c2 = r2.cookies.get_dict()   #第二次获取cookies
c1.update(c2)    #cookies的更新

r3 = requests.post("https://github.com/settings/repositories",cookies=c1)
soup2 = BeautifulSoup(r3.text,features="lxml")
  
print(soup2)

 

posted @ 2019-04-23 22:50  souwote  阅读(266)  评论(0编辑  收藏  举报