scrapy模拟登陆强智教务系统

本文介绍了使用scrapy框架模拟登录强智教务系统时遇到的302重定向问题及解决方案,包括在请求中处理重定向和解析响应头中的Location URL。此外,还提到了验证码识别问题,目前采用手工识别,但可通过调用验证码识别API实现自动化。

scrapy模拟登陆强智教务系统

引言

前面已经分享过用requests库来模拟登陆强智,这次打算用scrapy框架试一下,结果遇到的坑还真不少。

思路分析

前面已经分享过了,这里就不重复造轮子了,请出门,然后右转。传送门
这篇文章的主要目的是记录用scrapy框架模拟登陆强智教务系统遇到的问题和解决方法。

问题分析

首先,遇到的最大问题就是302重定向,scrapy框架默认是不能处理302重定向的,需要在发送请求时,加入dont_redirect

meta={
   
   'dont_redirect': True, 'handle_httpstatus_list': [302]}

然后,需要在响应头中拿到Location的URL,这里要注意,拿到的URL是bytes类型,需要转换成str类型才能使用(decode一下就好了)。

location_url = bytes.decode(response.headers.getlist('Location')[0])

最后,验证码识别的问题,这里采用的是手工识别,如果想要实现自动化,可以去网上找验证码的识别平台,直接调用API接口即可。

项目目录

在这里插入图片描述

代码

settings.py

# -*- coding: utf-8 -*-

# Scrapy settings for sdust project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://docs.scrapy.org/en/latest/topics/settings.html
#     https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://docs.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'sdust'

SPIDER_MODULES = ['sdust.spiders']
NEWSPIDER_MODULE = 'sdust.spiders'

# Crawl responsibly by identifying yourself (and your website) on the user-agent
# USER_AGENT = 'sdust (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
# CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
# DOWNLOAD_DELAY = 1
# The download delay setting will honor only one of:
# CONCURRENT_REQUESTS_PER_DOMAIN = 16
# CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
# COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
# TELNETCONSOLE_ENABLED = False

# Override the default request headers:
DEFAULT_REQUEST_HEADERS = {
   
   
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language': 'en',
    'User-Agent': 'Mozilla/5.0 (Windows NT
Scrapy是一个大的Python网络爬虫框架,它可以帮助开发者抓取网站数据。模拟登录Scrapy的过程通常涉及以下几个步骤: 1. **设置请求头**:为了模拟真实用户,需要设置合适的User-Agent、Cookie等请求头信息。这可以防止目标站点识别出是爬虫。 ```python headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' } ``` 2. **创建登录请求**:创建一个登录页面的GET请求,获取初始的登录表单数据,如CSRF Token等。 ```python response = requests.get(login_url, headers=headers) login_form_data = dict(response.form.fields) ``` 3. **构造POST数据**:根据登录页面的HTML结构,填充用户名、密码和其他必填字段,并加入之前获取到的CSRF Token。 ```python data = { 'username': username, 'password': password, # 根据实际页面填写其他字段 'csrfmiddlewaretoken': login_form_data['csrfmiddlewaretoken'] } ``` 4. **发送登录请求**:使用Scrapy的`FormRequest`类发送POST登录请求。 ```python form_request = FormRequest( url=login_url, method='POST', formdata=data, headers=headers, callback=self.handle_login_response, dont_filter=True, # 防止Spider从过滤列表中移除 ) ``` 5. **处理响应**:定义`handle_login_response`函数来检查登录是否成功,如果成功,你可以继续抓取需要的数据;如果失败,则需要分析错误原因并尝试修复。 ```python def handle_login_response(self, response): if 'You are logged in.' in response.text: # 登录成功,开始抓取内容 self.crawl_urls_to_scrape() else: # 处理登录失败情况... ``` 6. **添加登录请求到Spiders**:在Scrapy Spider中,将登录请求添加到待抓取队列中。 ```python spider = MySpider() spider.start_requests.append(form_request) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值