导语:
本文主要介绍了关于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电脑。
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ python中矩阵除法运算的三种实现方法08/12
- ♥ python装饰器的实现方法12/23
- ♥ 哪些人适合学习Python?12/04
- ♥ 如何在 python 3.6 中保存编辑的代码12/09
- ♥ 6个常用的顶级Python文本编辑器09/01
- ♥ 如何将元素插入到python中的列表中10/16
内容反馈