Excluding web links with specific extensions in web scraper
Question: I need to exclude printing links in my web scraper that end in .od .jpg .pdf
or .mp3

Here's my `if` statement

    
    
    if link in linkList():
          print link
    

Is there some library in Python for that? I only know of"RegEx" but I'm not
the greatest user of it.


Answer: Assuming that your link is just the path, you can do something like the
following:

    
    
    import os
    if os.path.splitext(link)[1] not in ['.jpg', '.pdf', '.mp3']:
        print link
    

The function `splitext` takes a path and returns a tuple containing the path
without the extension, followed by the extension. For example:

    
    
    >>> os.path.splitext('http://www.example.com/path/to/filename.ext')
    ('http://www.example.com/path/to/filename', '.ext')
    

So if you split the link with that function, you can check whether the last
element of the tuple is a member of another list/set/tuple containing your
blacklist of extensions.

