在废弃数据之前,Scrapy会重定向到注销

时间:2018-03-27 06:45:27

标签: python python-3.x scrapy scrapy-spider

标题可能有点令人困惑,但让我解释一下。我正在尝试使用scrapy构建一个简单的刮刀来刮取银行网站的一些自动预算。到目前为止,似乎我可以登录,但在我退出后立即没有获得我需要的数据。这是我终端上的一些文字:

1. 2018-03-27 00:56:56 [scrapy.core.engine] DEBUG: Crawled (200) <POST 
   https://www.bank.org/signin-page.html> (referer: 
   https://www.bank.org/signin-page.html)
2. 2018-03-27 00:56:56 [LOG] INFO: LOGIN ATTEMPT SUCCESSFUL
3. 2018-03-27 00:56:56 [scrapy.core.engine] DEBUG: Crawled (404) <GET 
   https://www.bankonline.org/robots.txt> (referer: None)
4. 2018-03-27 00:56:56 [scrapy.downloadermiddlewares.redirect] DEBUG: 
   Redirecting (302) to <GET https://www.bankonline.org/tob/live/usp- 
   core/app/logout?reason=logout> from <GET 
   https://www.bankonline.org/tob/live/usp-core/app/home>
5. 2018-03-27 00:56:56 [scrapy.core.engine] DEBUG: Crawled (200) <GET 
   https://www.bankonline.org/tob/live/usp-core/app/logout? 
   reason=logout> (referer: https://www.bank.org/signin-page.html)
6. 2018-03-27 00:56:56 [LOG] INFO: VISITED 
   https://www.bankonline.org/tob/live/usp-core/app/logout? 
   reason=logout
7. 2018-03-27 00:56:57 [scrapy.core.engine] INFO: Closing spider 
   (finished)

第4行是开始重定向我的地方。这是我的代码:

import scrapy
import logging

logger = logging.getLogger('LOG')
USERNAME = 'user'
PASSWORD = 'pass'

class Budget_Bank(scrapy.Spider):
    name = "Budget_Bank"
    login_url = 'https://www.bank.org/signin-page.html'
    start_urls = ['https://www.bank.org/signin-page.html']

    def parse(self, response):
        yield scrapy.FormRequest(url=self.login_url,
                                 formdata={'username': USERNAME,
                                           'password': PASSWORD},
                                 callback=self.login_test)


    def login_test(self, response):
        if 'errors' in response.text:
            logger.warning("LOGIN ATTEMPT FAILED")
            return
        else:
            logger.info("LOGIN ATTEMPT SUCCESSFUL")
            yield scrapy.Request('https://www.bankonline.org'
                                 '/tob/live/usp-core/app/home',
                                 callback=self.parse_number)


    def parse_number(self, response):
        logger.info("VISITED %s", response.url)
        for number in response.css('div._1qtcLoK1d4PZmeghcgyE2K'):
            yield {
                'num': number.css('span.formattedMoney_balanceBZozG-'         
                                  ...::text').extract_first(),
            }

我到目前为止只是试图从网站上获取一个数字来测试我是否可以实际检索数据。我的login_test返回我正确登录,但不是继续到主页面,而是重定向到logout。由于显而易见的原因,我省略了一些信息,例如我的用户名和密码,还更改了网站名称。一些帮助将非常感激。

1 个答案:

答案 0 :(得分:0)

您被重定向到注销,因为它检测到您是机器人。

3. 2018-03-27 00:56:56 [scrapy.core.engine] DEBUG: Crawled (404) <GET 
   https://www.bankonline.org/robots.txt> (referer: None)

您可以尝试将ROBOTSTXT_OBEY设置为False

有关详细信息,请参阅Doc