user61629 user61629 - 1 year ago 74
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 ( 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 Source

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__()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download