本篇文章小编给大家分享一下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://g*i*th*ub.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': '[email protected]',
'password': '123456',
'webauthn-support': 'supported',
'authenticity_token': authenticity_token,
'utf8': utf8,
'commit': commit}
yield scrapy.FormRequest("https://gi*thub*.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.websc***raping.com/user/profile']
login_url = 'http://example.we*bs**craping.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':'[email protected]','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()
摩托车销售模拟器内置菜单中文版
摩托车出售模拟器,又名摩托车销售模拟器,这是一个以摩托车销售
船舶模拟2020最新版
船舶模拟2020是玩法非常有意思的模拟驾驶游戏,高清3d画质
油管主播的生活2内置菜单版
油管主播的生活2内置菜单版是一款模拟养成类游戏,在这里你将体
网吧老板模拟器2手机版
网吧老板模拟器2是非常受欢迎的模拟经营题材手游,在原本的基础
美国警察模拟器巡警2024最新版
美国警察模拟器是一款好玩的模拟手游,游戏中你将扮演一位警察,