admin管理员组文章数量:1221306
When using FormRequest.from_response, Scrapy sends all the form fields in the request. We can override value of a input field by defining in formdata, but I need to exclude a field.
How can I exclude a field, or force scrapy to skip a particular field in the request payload. Since I'm dealing with ASP.NET pages, I need the from_response to handle the various ASP parameters.
When using FormRequest.from_response, Scrapy sends all the form fields in the request. We can override value of a input field by defining in formdata, but I need to exclude a field.
How can I exclude a field, or force scrapy to skip a particular field in the request payload. Since I'm dealing with ASP.NET pages, I need the from_response to handle the various ASP parameters.
Share Improve this question asked Feb 7 at 20:02 Alex KeyAlex Key 496 bronze badges1 Answer
Reset to default 0You can modify your request body in process_request
method of a downloadermiddleware
, and enable it in spider settings.
Spider:
class TestSpider(scrapy.Spider):
name = 'test'
custom_settings = {
"DOWNLOADER_MIDDLEWARES": {
'xy_spider.middlewares.TestDownloaderMiddleware': 200,
},
}
def start_requests(self):
yield scrapy.FormRequest(
'https://postman-echo.com/post',
formdata={'a': '1', 'b': '2'} # two fields in formdata
)
def parse(self, response):
print(response.text)
And middlewares.py
:
class TestDownloaderMiddleware:
@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s
def process_request(self, request, spider):
body = {p.split('=')[0]: '='.join(p.split('=')[1:]) for p in request.body.decode('utf8').split('&')}
if 'b' in body: # here you can exclude `b` field
body.pop('b')
return request.replace(body=''.join([f"{k}={v}" for k, v in body.items()]))
return None
本文标签: Scrapy Exclude input field in fromresponseStack Overflow
版权声明:本文标题:Scrapy: Exclude input field in from_response - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739292580a2156758.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论