Scrapy - 从 javascript 脚本响应中检索身份验证令牌

Scrapy - Retrieve Authentication Token from javascript script response

我需要有关此特定情况的帮助。

场景

  1. 呼叫站点

http://www.example.com/index.php

我可以从 <script> 标签中获取此信息

https://www.example.com/anotherpage.php?key=ABCDFG

使用密钥,我必须调用此端点

https://www.example.com/login.php?key=ABCD

用于检索存储在 javascript 响应中的 SessionID

-- omitted

private._sessID='MYSESSIONID';

-- omitted

最后,使用此 sessionId 并执行正确的 POST 操作,我可以在我需要的所有页面内导航。

我的僵局

我能够使用 scrapy shellregEx 模拟所有步骤(并且一切正常),但我不知道如何在开始数据之前在 scrapy 蜘蛛中管理这些步骤提取.

有人可以帮我吗?

您需要从基础 URL http://www.example.com/index.php 开始,方法是在启动请求方法中调用它并编写其回调并从其他端点提取信息并将该结果带入其他回调,然后您可以开始抓取过程。

您需要按照以下方式实现

class CrawlSpider(scrapy.CrawlSpider):

   def parse_authentication_token(self, response):
      //extract token or whatever require and then call supers parse
      yield from super().parse()

   def start_request(self):
       return Request(url, callback=self.parse_authentication_token)