python scrapy模拟登录的方法

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

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

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

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

3、scrapy直接带cookies。

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

#-*-coding:utf-8-*-
importscrapy
importre

classGithubLoginSpider(scrapy.Spider):
name='github_login'
allowed_domains=['github.com']
start_urls=['https://github.com/login']

defparse(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}
yieldscrapy.FormRequest("https://github.com/session",formdata=form_data,callback=self.after_login)

defafter_login(self,response):#验证是否请求成功
print(re.findall('LearnGitandGitHubwithoutanycode!',response.body.decode()))

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。