• 技术文章 >Python技术 >Python基础教程

    python scrapy模拟登录的方法

    小妮浅浅小妮浅浅2021-08-14 10:07:52原创2793

    1、requests模块。直接携带cookies请求页面。

    找到url,发送post请求存储cookie。

    2、selenium(浏览器自动处理cookie)。

    找到相应的input标签,输入文本,点击登录。

    3、scrapy直接带cookies。

    找到url,发送post请求存储cookie。

    # -*- coding: utf-8 -*-
    import scrapy
    import re
     
    class GithubLoginSpider(scrapy.Spider):
        name = 'github_login'
        allowed_domains = ['github.com']
        start_urls = ['https://github.com/login']
     
        def parse(self, response): # 发送Post请求获取Cookies
            authenticity_token = response.xpath('//input[@name="authenticity_token"]/@value').extract_first()
            utf8 = response.xpath('//input[@name="utf8"]/@value').extract_first()
            commit = response.xpath('//input[@name="commit"]/@value').extract_first()
            form_data = {
                'login': 'pengjunlee@163.com',
                'password': '123456',
                'webauthn-support': 'supported',
                'authenticity_token': authenticity_token,
                'utf8': utf8,
                'commit': commit}
            yield scrapy.FormRequest("https://github.com/session", formdata=form_data, callback=self.after_login)
     
        def after_login(self, response): # 验证是否请求成功
            print(re.findall('Learn Git and GitHub without any code!', response.body.decode()))

    以上就是python scrapy模拟登录的方法,希望对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python scrapy
    品易云
    上一篇:python如何重写start_requests方法 下一篇:python scrapy.Request发送请求的方式

    相关文章推荐

    • python字符串对齐的三种方法• python字符串大小写转换的3种函数• python如何删除字符串的特殊字符• python两种数据类型的转换• python可变参数的两种传递方式• python import的本质探究• python Series如何进行相加• python如何查看hdf5文件• python scrapy如何建模• python scrapy处理翻页的方法• python如何重写start_requests方法

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网