-1
def check_links(hrefs_list):
    check_str = ['NEWS',
                 'MEDIA',
                 'news',
                 'media',
                 'News',
                 'Media',
                 'press',
                 'releases',
                 'Press',
                 'Releases',
                 'PRESS',
                 'RELEASES']
    relevant_links=list()
    for i in range(len(hrefs_list)):
        if hrefs_list[i].find(check_str)!= -1:
            relevant_links.append(hrefs_list[i])
        else:
            continue
    return relevant_links

check_links(hrefs_list) accepts a list of href values. I want to go to each href value and check if it contains any of the keyword present in the check_str list. If it does, I want to store it to a fresh list, otherwise not. But when I run this function it gives an error : if hrefs_list[i].find(check_str)!= -1: TypeError: must be str, not list

How do I solve this ?

Soumya Pandey
  • 321
  • 3
  • 19

1 Answers1

0

You should use this:

def check_links(hrefs_list):
    check_str = ['NEWS',
                 'MEDIA',
                 'news',
                 'media',
                 'News',
                 'Media',
                 'press',
                 'releases',
                 'Press',
                 'Releases',
                 'PRESS',
                 'RELEASES']
    relevant_links=list()
    for i in range(len(hrefs_list)):
        if hrefs_list[i] in check_str:
            relevant_links.append(hrefs_list[i])
        else:
            continue
    return relevant_links

Example:

hrefs_list = ['NEWS',
              'MEDIA',
              'news',
              'others']
check_links(hrefs_list)

Output:

['NEWS', 'MEDIA', 'news']
itsDV7
  • 854
  • 5
  • 12