0
link = 'http://dedegood.com'
wrongdomain = ['google','facebook','twitter']


if any(link.find(i) for i in wrongdomain):
    print 'pass this url'
else:
    print 'good'

I want to check if link contains the words in wrongdomain
Why this always print 'pass this url'?
link has no google or facebook or twitter in it
I try seperate like link.find('google')
it will return -1 .so what's the problem?

Please help me to check my logic.Thank you

3 Answers 3

2

bool(-1) is True in Python. Instead of find, you can just do:

if any(domain in link for domain in wrongdomain):

Just remember that will also match the rest of the url, not just the domain.

Sign up to request clarification or add additional context in comments.

Comments

0

Your method will not work correctly like a url like http://dedegood.com/google this. So you can use something like;

link = 'http://dedegood.com'
wrongdomain = ['google','facebook','twitter']

a=link.split("//")
b=a[1].split(".")
if any(domain in b[0] for domain in wrongdomain):
     print ('pass this url')
else:
    print ('good')

Since you just want to check url, you can use this one. Instead of checking all link, it's checking only the name of website. So if any url like http://dedegood.com/google will not be a problem.

Comments

0

Do you want to know whether the url's domain is in wrongdomain or not? I would suggest you can do this for better performance:

import urlparse
import tldextract

link = 'http://dedegood.com'
wrongdomain = ['google','facebook','twitter']
parsed = tldextract.extract(link)
if parsed.domain in wrongdomain:
    print 'pass this url'
else:
    print 'good'

You could check out tldextract, a library designed to get domain from a url.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.