• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    python scrapy简单模拟登录的代码分析

    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()))

    知识点扩展:

    parse_login方法是提交完表单后callback回调函数指定要执行的方法,为了验证是否成功。这里我们直接在response中搜索Welcome Liu这个字眼就证明登录成功。

    这个好理解,重点是yield from super().start_resquests(),这个代表着如果一旦登录成功后,就直接带着登录成功后Cookie值,方法start_urls里面的地址。

    这样的话登录成功后的response可以直接在parse里面写。

    # -*- coding: utf-8 -*-
    import scrapy
    from scrapy import FormRequest,Request
    
    
    class ExampleLoginSpider(scrapy.Spider):
        name = "login_"
        allowed_domains = ["example.webscraping.com"]
        start_urls = ['http://example.webscraping.com/user/profile']
        login_url = 'http://example.webscraping.com/places/default/user/login'
    
        def parse(self, response):
            print(response.text)
    
        def start_requests(self):
            yield scrapy.Request(self.login_url,callback=self.login)
    
        def login(self,response):
            formdata = {
                'email':'liushuo@webscraping.com','password':'12345678'}
            yield FormRequest.from_response(response,formdata=formdata,
                                            callback=self.parse_login)
        def parse_login(self,response):
            # print('>>>>>>>>'+response.text)
            if 'Welcome Liu' in response.text:
                yield from super().start_requests()

    到此这篇关于python scrapy简单模拟登录的代码分析的文章就介绍到这了,更多相关python scrapy模拟登录的方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    您可能感兴趣的文章:
    • python爬虫框架scrapy实现模拟登录操作示例
    上一篇:python数据可视化之matplotlib.pyplot基础以及折线图
    下一篇:Opencv实现二维直方图的计算及绘制
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    python scrapy简单模拟登录的代码分析 python,scrapy,简单,模拟,登录,