0

I have a list of links and want to select some them based on list of subtrings into the link. I'd like to select the links with the substrings medalha or mencao-honrosa. Following a sample of selected links.

https://onciencias.org/resultado/detalhe/9-ano-ensino-fundamental/medalha-ouro
https://onciencias.org/resultado/detalhe/9-ano-ensino-fundamental/medalha-prata
https://onciencias.org/resultado/detalhe/9-ano-ensino-fundamental/medalha-bronze
https://onciencias.org/resultado/detalhe/9-ano-ensino-fundamental/mencao-honrosa
https://onciencias.org/resultado/detalhe/1-serie/medalha-ouro
https://onciencias.org/resultado/detalhe/1-serie/medalha-prata
https://onciencias.org/resultado/detalhe/1-serie/medalha-bronze
https://onciencias.org/resultado/detalhe/1-serie/mencao-honrosa
https://onciencias.org/resultado/detalhe/2-serie/medalha-ouro
https://onciencias.org/resultado/detalhe/2-serie/medalha-prata
https://onciencias.org/resultado/detalhe/2-serie/medalha-bronze
https://onciencias.org/resultado/detalhe/2-serie/mencao-honrosa
https://onciencias.org/resultado/detalhe/3-serie/medalha-ouro
https://onciencias.org/resultado/detalhe/3-serie/medalha-prata
https://onciencias.org/resultado/detalhe/3-serie/medalha-bronze
https://onciencias.org/resultado/detalhe/3-serie/mencao-honrosa
https://onciencias.org/resultado/detalhe/4-ano-tecnico/medalha-ouro
https://onciencias.org/resultado/detalhe/4-ano-tecnico/medalha-prata
https://onciencias.org/resultado/detalhe/4-ano-tecnico/medalha-bronze
https://onciencias.org/resultado/detalhe/4-ano-tecnico/mencao-honrosa

I did the following code and it's working. What's best another way to write this code and avoid to use or to separate the substrings.

for temp_link in list_links:
    if(str(temp_link).find('medalha') != -1 or str(temp_link).find('mencao-honrosa') != -1):
        links.append(url_home+temp_link[1:])
        print(url_home+temp_link[1:])

Thank you.

1
  • Be careful with using in, it behaves in a way that can seem surprising. For example, if I'm looking for the word 'cat', 'cat' in 'catastrophe' would return True.
    – AMC
    Jan 23, 2020 at 22:57

1 Answer 1

3

Try this,

[url for url in urls if 'medalha' in url or 'mencao-honrosa' in url]

or if you have a list of search terms, maybe something like this.

search_terms = ['medalha', 'mencao-honrosa']
[url for url in urls if any([term in url for term in search_terms])]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.