【发布时间】:2016-12-01 05:33:09
【问题描述】:
我是 scrapy 的新手,并试图从 fandromeda.com 网站上抓取一些数据,但我需要先进行身份验证,然后才能开始抓取以下 URL 上的所需数据
https://fandromeda.com/v2/event/results
我正在尝试使用 from_response 方法来尝试登录网站
import scrapy
from scrapy.spiders import CrawlSpider
from scrapy.http import Request, FormRequest
from scrapy.selector import Selector
from fandromeda.items import FandromedaItem
class FandromedaC(CrawlSpider):
name = 'fandromeda_c'
allowed_domains = ['fandromeda.com']
start_urls = ['https://fandromeda.com/user/signin']
def parse(self, response):
sel = Selector(response)
sign_in = sel.xpath('//button[@type="submit"]/text()').extract()
if sign_in:
if sign_in[0] == "SIGN IN":
self.log("$$$$$$$$$$$$$$$$$$")
form_data = {'username': 'xxxx','password': 'xxxx' }
return scrapy.FormRequest.from_response(response,formdata=form_data,callback=self.parse_login_response)
def parse_login_response(self,response):
self.log("#####################")
self.log(response.status)
我希望被重定向到主页并获得该内容作为响应,但我得到的只是再次登录页面。
我注意到在默认情况下,scrapy 正在发出获取请求而不是发布。 2016-12-01 10:53:44 [scrapy] 调试:已爬网 (200) https://fandromeda.com/user/signin?handle=&username=xxxx&password=xxxx>(引用者:https://fandromeda.com/user/signin)
我尝试在响应中使用方法参数来强制scrapy使用POST方法
scrapy.FormRequest.from_response(response,method="POST",formdata=form_data,callback=self.parse_login_response)
现在,scrapy 发出了 post 请求,但结果有所不同。 有人可以指出我需要做什么的正确方向吗?
【问题讨论】:
标签: authentication scrapy http-post