I have a list of patterns:
patterns_trees = [response.css("#Header").xpath("//a/img/@src"),
response.css("#HEADER").xpath("//a/img/@src"),
response.xpath("//header//a/img/@src"),
response.xpath("//a[@href='"+response.url+'/'+"']/img/@src"),
response.xpath("//a[@href='/']/img/@src")
]
After I traverse it and find the right pattern I have to send the pattern as an argument to a callback function
for pattern_tree in patterns_trees:
...
pattern_response = scrapy.Request(...,..., meta={"pattern_tree": pattern_tree.extract_first()})
By doing this I get the value of the regex not the pattern
THINGS I TRIED:
I tried isolating the patterns in a separate class but still I have the problem that I can not store them as pattern but as values.
I tried to save them as strings and maybe I can make it work but
What is the most efficient way of storing list of functions
UPDATE: Possible solution but too hardcoded and it's too problematic when I want to add more patterns:
def patter_0(response):
response.css("#Header").xpath("//a/img/@src")
def patter_1(response):
response.css("#HEADER").xpath("//a/img/@src")
.....
class patternTrees:
patterns = [patter_0,...,patter_n]
def length_patterns(self):
return len(patterns)
metayou're sending.extract_first(), the brackets cause it to execute. Try sending.extract_firstinstead (without brackets) to send the actual function.pattern_treeis already a computed resultresponseis an instance of to access its methods. For strings you can do this:[str.upper, str.lower][0]('test')->'TEST'