这次我们要模拟登录的页面是

https://github.com/login

首先我们先尝试着登陆一遍分析一下请求,

打开开发者工具下的network选项,

可以很清楚的看到这个会话session,而且是post请求,这多半是我们刚才登录是所发出去的请求,我们往下拉查看form data

这里要注意,这个authenticity_token的值该怎么获取,我们从网页的源代码中查找这个关键字,

发现这个值就在页面中,而且每次刷新之后这个value总是会不断地变化,好,分析完毕后,我们开始构建代码

 import requests
from bs4 import BeautifulSoup
import time
#自己写的,模块化一哈,舒服一哈,2019.5.6 22.22
#模拟登陆练习,登录github
class login(object):
def __init__(self):
self.headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'
}
self.first_url='https://github.com/login'
self.second_url='https://github.com/session'
self.test_url='https://github.com/?tab=stars' #可以设置成你的个人主页url
self.session=requests.Session()
def get_log(self):#这个方法是把token信息解析找到后传递
html=requests.get(url=self.first_url,headers=self.headers)
soup=BeautifulSoup(html.text,'lxml')
tokens=soup.find_all('input',type="hidden")[1]
at=tokens.attrs['value']
return at
def post_log(self,login,password):
       #这是要提交的表单
data={
'commit':'Sign in',
'utf8':' ✓',
'authenticity_token':self.get_log(),
'login':login,
'password':password,
'webauthn-support':' supported'
}
res=self.session.post(url=self.second_url,data=data,headers=self.headers)
self.test_func()
def test_func(self): #这个方法用来模拟登陆后请求我们的个人主页,然后把源码保存到文件里,以此来确定是否模拟登录成功。
res2=self.session.get(url=self.test_url,headers=self.headers)
with open('gitstar.html','wb') as f:
f.write(res2.content)
if __name__=='__main__':
jessus=login()
jessus.post_log('你的用户名','你的密码')

运行一下,我们把保存的html文件打开

成功了,确实是我们的github主页!

希望能给你提供思路,主要是requests post 请求,还有维持一个会话session很重要,如果你觉得文章还不错,点个赞吧,谢谢!

python学习,使用requests库来模拟登录github,post请求。的更多相关文章

  1. python学习(23)requests库爬取猫眼电影排行信息

    本文介绍如何结合前面讲解的基本知识,采用requests,正则表达式,cookies结合起来,做一次实战,抓取猫眼电影排名信息. 用requests写一个基本的爬虫 排行信息大致如下图 网址链接为ht ...

  2. [Python] Python 模拟登录,并请求

    Python 模拟登录,并请求 # encoding: utf- import requests import socket import time socket.setdefaulttimeout( ...

  3. Python学习day45-数据库(总结)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  4. Python学习day42-数据库的基本操作(1)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  5. Python学习day41-数据库(1)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  6. python下载安装requests库

    一.python下载安装requests库 1.到git下载源码zip源码https://github.com/requests/requests 2.解压到python目录下: 3.“win+R”进 ...

  7. Python学习day44-数据库(单表及多表查询)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  8. Python学习day43-数据库(多表关系)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  9. 使用requests库提交multipart/form-data 格式的请求

    前言: Requests是用Python语言编写,基于urllib,采用Apache2 Licensed开源协议的HTTP库.它比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求 ...

随机推荐

  1. php语法错误导致服务器错误(500)解决

    PHP编码出错不提示,而是提示500错误,这对于开发来说,是很不方便的.下面讲解如何开启错误提示步骤: 1. 打开php.ini文件.以我的ubuntu为例,这个文件在: /etc/php5/apac ...

  2. uva10828

    https://vjudge.net/problem/UVA-10828 裸高斯消元... 但是要判无解和无穷解. 当出现一个环时会无解,环上每个点只有一个出边. #include<bits/s ...

  3. 用 python 写一个模拟玩家移动的示例

    实例:二维矢量模拟玩家移动 在游戏中,一般使用二维矢量保存玩家的位置,使用矢量计算可以计算出玩家移动的位置,下面的 demo 中,首先实现二维矢量对象,接着构造玩家对象,最后使用矢量对象和玩家对象共同 ...

  4. 论文翻译-SELF TRAINING AUTONOMOUS DRIVING AGENT

    文献地址 链接:https://pan.baidu.com/s/1gHrpnOf1FXLp9u8OJ2-oCg 提取码:y2w6 作者 Shashank Kotyan, Danilo Vasconce ...

  5. golang——字符串与编码

    1.字符编码 (1)ASCII码 一个字节表示的英文.数字.标点符号等字符. 国际标准ASCII码为0-127即128个字符,二进制最高位为0,其余为扩展ASCII码. (2)GB2312 两字节,主 ...

  6. MySQL故障处理一例_Another MySQL daemon already running with the same unix socket

    MySQL故障处理一例:"Another MySQL daemon already running with the same unix socket". [root@test- ...

  7. CSS之选择符、链接、盒子模型、显示隐藏元素

    <html> <head> <meta charset="utf-8"> <title>选择符.链接.盒子模型.显示隐藏元素< ...

  8. Cesium加载影像

    注意:使用自定义数据源时,Cesium.Viewer类参数必须设置为 baseLayerPicker:false A. 使用天地图数据源 //天地图var provider=new Cesium.We ...

  9. LN : leetcode 690 Employee Importance

    lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...

  10. Android Could not find com.afollestad:material-dialogs:0.7.6.0 解决

    AS报错:Could not find com.afollestad:material-dialogs:0.7.6.0 网上没有解决方案: 解决: 将用: compile('com.afollesta ...