栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python模拟登录

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python模拟登录

比较不同模拟登录的方法
  • 一、原理
  • 二、普通requests获取session进行登录
  • 三、Scrapy模拟登录

一、原理

比较cookies和session的区别:

cookies的数据信息存放在客户端浏览器上,而session的数据信息存放在服务器上。

通过获取session可实现以登录的状态获取用户资料

本次模拟登录对象为:https://github.com/,以开发者模式登录账号获取一个session


以下为表单数据,需要传入网址

原理不难,现在可着手开始工作

二、普通requests获取session进行登录
import requests
import re
from lxml import etree

class Login(object):
    def __init__(self):
        self.headers={
            'referer':'https://github.com/',
            'user-agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) '
                         'Chrome/91.0.4472.114 Mobile Safari/537.36',
            'host':'github.com'
        }
        self.login_url='https://github.com/login'#登录地址
        self.post_url='https://github.com/session'#会话地址
        
        self.session=requests.Session()#维持会话
    def token(self):
        resp=self.session.get(self.login_url,headers=self.headers)
        selector=etree.HTML(resp.text)
        token=selector.xpath('//*[@id="login"]/div[4]/form/input[1]/@value')[0]#获取表单数据中authenticity_token参数
        return token
    def login(self,email,password):
        post_data={
            'commit':'Sign in',
            'authenticity_token':self.token(),
            'login':email,
            'password':password
        }#构造表单,传入网址
        resp=self.session.post(self.post_url,data=post_data,headers=self.headers)
        print(re.findall('lx-2021', resp.body.decode()))#正则表达式输出内容
if __name__=='__main__':
    login=Login()
    login.login(email='账号',password='密码')#输入自己的github账号以及密码
三、Scrapy模拟登录
import scrapy
import re
from scrapy import FormRequest

class GtSpider(scrapy.Spider):
    name = 'gt'
    allowed_domains = ['github.com']
    start_urls = ['http://github.com/login']

    '''
    注释内容也可以实现模拟登录,通过FormRequest.from_response
    自动获取action,即跳转页面。
    '''
    # def parse(self, response):
    #     yield FormRequest.from_response(
    #         response,
    #         formdata={'login':'123456789','password':'123456789'},
    #         callback=self.parse_item #回调函数
    #
    #     )
    def parse(self, response):
        authenticity_token=response.xpath('//input[@name="authenticity_token"]/@value').extract_first()
        commit=response.xpath('//input[@name="commit"]/@value').extract_first()
        formdata=dict(
            login='2725909286@qq.com',
            password='13049273494a',
            authenticity_token= authenticity_token,
            commit=commit
        )
        yield FormRequest(
            'http://github.com/session',
            formdata=formdata,
            callback=self.parse_item
        )

    def parse_item(self,response):
        print(re.findall('lx-2021',response.body.decode()))

通过正则获取到了个人昵称,表示登录成功!


注意:GitHub网址打开看运气,有时候打得开,有时候打不开。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325763.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号