user61629 user61629 - 1 month ago 6
Python Question

Test for presence of specific text in tag with scrapy

I'm interested in testing for the presence of specific text (an error message) on pages that I'm scraping. I have the following working statement:

not_found=response.selector.xpath('//*[@id="Error"]/font[contains(text(),"is not found")]')


I can see that their is a boolean test mentioned in the docs (https://doc.scrapy.org/en/0.12/topics/selectors.html#scrapy.selector.XPathSelector.nonzero) but I'm not sure if I can apply it here to do something like (pseudocode):

if __nonzero__(not_found):


I was having trouble finding it to import so have not been able to test properly. How can I get this working?

Answer

Python doc: __nonzero__

It is used automatically when you do bool(not_found)
and bool() is use automatically when you do if not_found:


You don't have to use __nonzero__ and you shouldn't use it directly (because you have bool() for this) but if you really have to then it should be not_found.__nonzero__()