我試圖用Scrapy登錄,但收到很多「重定向(302)」消息。當我使用我的真實登錄信息和假登錄信息時,會發生這種情況。我也嘗試過與另一個網站,仍然沒有運氣。Scrapy FormRequest登錄不起作用
import scrapy
from scrapy.http import FormRequest, Request
class LoginSpider(scrapy.Spider):
name = 'SOlogin'
allowed_domains = ['stackoverflow.com']
login_url = 'https://stackoverflow.com/users/login?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com%2f'
test_url = 'http://stackoverflow.com/questions/ask'
def start_requests(self):
yield Request(url=self.login_url, callback=self.parse_login)
def parse_login(self, response):
return FormRequest.from_response(response, formdata={"email": "XXXXX", "password": "XXXXX"}, callback=self.start_crawl)
def start_crawl(self, response):
yield Request(self.test_url, callback=self.parse_item)
def parse_item(self, response):
print("Test URL " + response.url)
我也嘗試添加
meta = {'dont_redirect': True, 'handle_httpstatus_list':[302]}
最初的請求和FormRequest。
這裏是從上面的代碼的輸出:
2017年4月17日21點48分17秒[scrapy.utils.log] INFO:Scrapy 1.3.3開始 (BOT:計算器)2017- 04-17 21:48:17 [scrapy.utils.log]信息: 重寫設置:{'BOT_NAME':'stackoverflow','NEWSPIDER_MODULE': 'stackoverflow.spiders','SPIDER_MODULES':['stackoverflow.spiders '], 'USER_AGENT':'Mozilla/5.0'} 2017-04-17 21:48:17 [scrapy.middleware] 信息:啓用擴展:['scrapy.extensions.corestats.CoreStats', 'scrapy。 extensions.telnet.TelnetConsole', 'scrapy.extensions.logstats.LogStats'] 2017-04-17 21:48:17 [scrapy.middleware]信息:啓用下載中間件: ['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.downloadermiddlewar es.cookies.CookiesMiddleware', 'scrapy.downloadermiddlewares.stats.DownloaderStats'] 2017-04-17 21:48:17 [scrapy.middleware]信息:啓用蜘蛛中間件: ['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware 」, 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware', 'scrapy.spidermiddlewares.referer.RefererMiddleware', 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware', 'scrapy.spidermiddlewares.depth.DepthMiddleware'] 2017-04- 17 21:48:17 [scrapy.middleware]信息:啓用項目管道:[] 2017-04-17 21:48:17 [scrapy.core.engine]信息:蜘蛛打開2017-04-17 21:48 :17 [scrapy.extensions.logstats]信息:已檢索0頁(0頁/分鐘), 刮0項(在0項/分鐘)2017-04-17 21:48:17 [scrapy.extensions.telnet]調試:Telnet控制檯上偵聽 127.0.0.1:6023 2017-04-17 21:48: 18 [scrapy.core.engine] DEBUG:Crawled(200)https://stackoverflow.com/users/login?ssrc=head & returnurl = http%3a%2f%2fstackoverflow.com%2f> (referer:無) 2017-04-17 21:48:18 [scrapy.core.engine] DEBUG: Crawled(200)https://stackoverflow.com/search?q= & email = XXXXX & password = XXXXX> (referer: https://stackoverflow.com/users/login?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com%2f) 2017-04-17 21:48:19 [scrapy.downloadermiddlewares.redirect] DEBUG: 將(302)重定向到http://stackoverflow.com/users/login?ssrc=anon_ask & returnurl = http%3a%2f%2fstackoverflow.com%2fquestions%2fask> from http:// stackoverflow。com/questions/ask> 2017-04-17 21:48:19 [scrapy.downloadermiddlewares.redirect] DEBUG:Redirecting(302)to https://stackoverflow.com/users/login?ssrc=anon_ask & returnurl = http%3%2f%2fstackoverflow.com%2fquestions%2fask> from http://stackoverflow.com/users/login?ssrc=anon_ask & returnurl = http%3a%2f%2fstackoverflow.com%2fquestions%2fask> 2017 -04-17 21:48:19 [scrapy.core.engine] DEBUG:Crawled(200)https://stackoverflow.com/users/login?ssrc=anon_ask & returnurl = http%3a%2f%2fstackoverflow.com% 2fquestions%2fask> (referer: https://stackoverflow.com/search?q=&email=XXXXX&password=XXXXX)Test URL https://stackoverflow.com/users/login?ssrc=anon_ask&returnurl=http%3a%2f%2fstackoverflow.com%2fquestions%2fask 2017-04-17 21:48:19 [scrapy .core.engine]信息:關閉蜘蛛 (完成)2017-04-17 21:48:19 [scrapy.statscollectors]信息:傾銷 Scrapy統計信息:{'downloader/request_bytes':1772, 'downloader/request_count' :5,'downloader/request_method_count/GET': 5,'downloader/response_bytes':34543,'downloader/response_count': 5,'downloader/response_status_count/200':3, 'downloader/response_status_count/302': 2,'finish_reason': 'finished','finish_time':datetime.datetime(2017,4,17,18,48,19, 470354),'log_count/DEBUG':6,'log_count/INFO':7 , 'request_depth_max':2,'response_received_count':3, 'scheduler/dequeued':5,'scheduler/dequeued/memory':5, 'scheduler/enqueu ed':5,'scheduler/enqueued/memory':5, 'start_time':datetime.datetime(2017,4,17,18,48,17,386516)} 2017-04-17 21:48:19 [scrapy.core.engine] INFO:蜘蛛關閉 (完)
這解決了它,謝謝。 – user2313267