scrapy登录失败,cookie未启用错误(启用了cookie)

时间:2017-02-01 22:39:46

标签: python cookies scrapy login-script

我尝试使用scrapy通过登录访问此网站:http://vietmessenger.com/cgi-bin/signin.cgi

在我刮网站之前,我使用这个scrapy python代码登录。但是,即使尝试了许多不同的方法,我总是得到这个错误(在response.body中): 错误::将您的浏览器设置为接受 cookies ,重新加载并重试..

我可以确认,如果我在Safari浏览器中禁用cookie,我会收到同样的错误。我感谢你的帮助,我不知道还有什么可以尝试。

import scrapy
import re
import pickle
from scrapy.http import FormRequest


class LoginSpider(scrapy.Spider):
    name = "ebooks"
    start_urls = ["http://vietmessenger.com/cgi-bin/signin.cgi"]

    def parse(self, response):
      formdata = {"MEMBERID":"userid", "PASSWORD":"password"}
      yield FormRequest.from_response(
          response,
          formdata = formdata,
          callback = self.after_login)

    def after_login(self, response): 
        #check if login is successful
        print(response.status)
        if b'ERROR::' in response.body:
            self.logger.error('login failed')
            return

这是日志:

$ scrapy crawl ebooks
2017-02-01 14:01:32 [scrapy.utils.log] INFO: Scrapy 1.3.0 started (bot: tutorial)
2017-02-01 14:01:32 [scrapy.utils.log] INFO: Overridden settings: {'BOT_NAME': 'tutorial', 'NEWSPIDER_MODULE': 'tutorial.spiders', 'SPIDER_MODULES': ['tutorial.spiders'], 'ROBOTSTXT_OBEY': True, 'COOKIES_DEBUG': True}
2017-02-01 14:01:32 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.logstats.LogStats',
 'scrapy.extensions.corestats.CoreStats']
2017-02-01 14:01:32 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-02-01 14:01:32 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-02-01 14:01:32 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-02-01 14:01:32 [scrapy.core.engine] INFO: Spider opened
2017-02-01 14:01:32 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-02-01 14:01:32 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-02-01 14:01:32 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://vietmessenger.com/robots.txt> (referer: None)
2017-02-01 14:01:32 [scrapy.downloadermiddlewares.cookies] DEBUG: Received cookies from: <200 http://vietmessenger.com/cgi-bin/signin.cgi>
Set-Cookie: REFERER=; path=/; domain=vietmessenger.com;

2017-02-01 14:01:32 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://vietmessenger.com/cgi-bin/signin.cgi> (referer: None)
2017-02-01 14:01:32 [scrapy.downloadermiddlewares.cookies] DEBUG: Sending cookies to: <POST http://vietmessenger.com/cgi-bin/signin.cgi>
Cookie: REFERER=

2017-02-01 14:01:33 [scrapy.downloadermiddlewares.cookies] DEBUG: Received cookies from: <200 http://vietmessenger.com/cgi-bin/signin.cgi>
Set-Cookie: REFERER=http://vietmessenger.com/cgi-bin/signin.cgi; path=/; domain=vietmessenger.com;

2017-02-01 14:01:33 [scrapy.core.engine] DEBUG: Crawled (200) <POST http://vietmessenger.com/cgi-bin/signin.cgi> (referer: http://vietmessenger.com/cgi-bin/signin.cgi)
200
2017-02-01 14:01:33 [ebooks] ERROR: login failed
2017-02-01 14:01:33 [scrapy.core.engine] INFO: Closing spider (finished)
2017-02-01 14:01:33 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 932,
 'downloader/request_count': 3,
 'downloader/request_method_count/GET': 2,
 'downloader/request_method_count/POST': 1,
 'downloader/response_bytes': 11490,
 'downloader/response_count': 3,
 'downloader/response_status_count/200': 3,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 2, 1, 22, 1, 33, 276611),
 'log_count/DEBUG': 7,
 'log_count/ERROR': 1,
 'log_count/INFO': 7,
 'request_depth_max': 1,
 'response_received_count': 3,
 'scheduler/dequeued': 2,
 'scheduler/dequeued/memory': 2,
 'scheduler/enqueued': 2,
 'scheduler/enqueued/memory': 2,
 'start_time': datetime.datetime(2017, 2, 1, 22, 1, 32, 620411)}
2017-02-01 14:01:33 [scrapy.core.engine] INFO: Spider closed (finished)

0 个答案:

没有答案