Search for list of keywords in python -


i've been able search keywords in strings before, i'm running problem using list so. keep getting error.

typeerror: 'in <string>' requires string left operand, not bool 

i've tried every solution think of, i'm @ right now:

from beautifulsoup import beautifulsoup import urllib2  keywords = ['diy','decorate', 'craft', 'home decor', 'food']  def get_tags(blog_soup):     tags_html = blog_soup.find('div', attrs = {'style': 'margin-left: 60px; margin-bottom: 15px;'})     tags = [tag.string tag in tags_html.findall('a')]     string_tags = str(' '.join(tags))     if any(keywords) in string_tags:         print url  url = 'http://technorati.com/blogs/blog.mjtrim.com' soup = beautifulsoup(urllib2.urlopen(url).read())  get_tags(soup) 

for minimal change working, can change any(keywords) in string_tags following:

any(keyword in string_tags keyword in keywords) 

or alternative using sets:

keywords = set(['diy','decorate', 'craft', 'home decor', 'food'])  def get_tags(blog_soup):     tags_html = blog_soup.find('div', attrs = {'style': 'margin-left: 60px; margin-bottom: 15px;'})     tags = [tag.string tag in tags_html.findall('a')]     if keywords.intersection(tags):         print url 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -