知行编程网知行编程网  2022-11-02 12:30 知行编程网 隐藏边栏  4 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于python scrapy模拟登录的方法的相关知识,希望可以帮到处于编程学习途中的小伙伴

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


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

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享