我使用 scrapy-inline-requests 库编写了一个蜘蛛。所以我的蜘蛛中的解析方法看起来像这样:
@inline_requests
def parse(self, response1):
item = MyItem()
loader = ItemLoader(item=item, response=response1)
#extracting some data from the response1
try:
response 2 = yield Request(some_url)
#extracting some other data from response2
except Exception:
self.logger.warning("Failed request to: %s", some_url)
yield loader.load_item()
我想有效地测试这个方法。我可以轻松编写一个测试,其中创建一个假模拟响应1并将其传递给函数。但是,我不知道如何模拟 response2 并使用两个虚假响应中的数据获取完整的项目。你有什么建议吗?
可能有点晚了,但请查看 scrapy-inline-requests 的 github 存储库中的测试:https://github.com/rmax/scrapy-inline-requests/blob/master/tests/test_inline_requests.py https://github.com/rmax/scrapy-inline-requests/blob/master/tests/test_inline_requests.py.
基本上,按照回调将产生这些响应的请求的顺序向回调提供响应对象列表。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)