zoukankan      html  css  js  c++  java
  • scrapy框架post模拟登录Github

    
    class GithubSpider(scrapy.Spider):
        name = 'github'
        allowed_domains = ['github.com']
        start_urls = ['https://github.com/login']
    
        def parse(self, response):
            username = ''#你的登录名
            password = ''#你的密码
    
            # 从登录页面响应中解析出post数据
            authenticity_token = response.xpath('//input[@name="authenticity_token"]/@value').extract_first()
            timestamp = response.xpath('//input[@name="timestamp"]/@value').extract_first()
            timestamp_secret = response.xpath('//input[@name="timestamp_secret"]/@value').extract_first()
            
            # 构造post表单数据
            post_data = {
                'commit': 'Sign in',
                'authenticity_token': authenticity_token,
                'login': username,
                'password': password,
                'trusted_device': '',
                'webauthn-support': 'supported',
                'webauthn-iuvpaa-support': 'unsupported',
                'return_to': '',
                'allow_signup': '',
                'client_id': '',
                'integration': '',
                'required_field_755d': '',
                'timestamp': timestamp,
                'timestamp_secret': timestamp_secret  
            }
            
            print(post_data)
            # 针对登录表单url发送post请求
            yield scrapy.FormRequest(
                url='https://github.com/session', 
                callback=self.after_login,
                formdata=post_data
            )
        
        def after_login(self, response):
            yield scrapy.Request('http://github.com/feijiang-cloud', callback=self.check_login)
        
        def check_login(self, response):
            """验证登录是否成功"""
            print('parse starting。。。')
            title = response.xpath('//head/title/text()').extract_first()
            print(title)
    
  • 相关阅读:
    OpenJudge 2764 数根 C++
    OpenJudge / Poj 1835 宇航员 C++
    elasticsearch系统性能调优总结
    ES基本查询总结
    Vim最全快捷键键位图
    Idea常用插件整合
    微信小程序开发资源汇总
    Java JNI调用本地动态库使用详解
    java开发调试定位分析工具大全
    Redux-saga使用教程详解
  • 原文地址:https://www.cnblogs.com/zxfei/p/14508197.html
Copyright © 2011-2022 走看看