[英]Python/Scrapy: Custom pipeline has no effect / download files with custom filename
[英]Scrapy Regex Custom Pipeline
这是我的Scrapy自定义正则表达式管道代码:
for p in item['code']:
for search_type, pattern in RegEx.regexp.iteritems():
s = re.findall(pattern, p)
if s:
return item
else:
raise DropItem
这是我的ReGex代码:
class RegEx(object):
regexp = {
'email' : re.compile('liczba'), 'whatever' : re.compile(r'mit'), 'blu' : re.compile(r'houseLocked'),}
不是真正的已编译正则表达式,仅用于演示目的。
这可行,但是一旦找到匹配项,并且触发了“返回项目”,其余的将被丢弃。
是否可以在Scrapy管道中继续迭代?
我已经进行了4天,尝试了您可以想象的所有排列,但始终得到相同的结果。
我要么错过明显的东西,要么就不简单了。
如果不可能以这种方式,对新路线的任何建议将不胜感激。
scrapy管道中的process_item()
方法应仅处理一项。 如果引发DropItem
或返回某些内容,则会中断循环并放弃其余的分析。
您的循环将在您进行的第一个正则表达式匹配之后中断,因为return item
和DropItem
中断循环并停止当前管道-换句话说,它将在第一个循环处中断。
要解决此问题,只需将DropItem
移动到主循环之外:
def process_item(self, item):
for p in item['code']:
for search_type, pattern in RegEx.regexp.iteritems():
if re.findall(pattern, p):
return item # one match found == item is valid, return
# if this is reached, it means no matches were found
# and we don't want this item
raise DropItem
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.