requests.exceptions.MissingSchema: Invalid URL (with bs4)
<p>I am getting this error:
requests.exceptions.MissingSchema: Invalid URL 'http:/1525/bg.png': No schema supplied. Perhaps you meant <a href="http://http:/1525/bg.png" rel="nofollow">http://http:/1525/bg.png</a>?</p>

<p>I don't really care why the error happened, I want to be able to capture any Invalid URL errors, issue a message and proceed with the rest of the code. </p>

<p>Below is my code, where I'm trying to use try/except for that specific error but its not working...</p>

<pre><code># load xkcd page
# save comic image on that page
# follow &lt;previous&gt; comic link
# repeat until last comic is reached

import webbrowser, bs4, os, requests

url = 'http://xkcd.com/1526/'
os.makedirs('xkcd', exist_ok=True)

while not url.endswith('#'): # - last page

    # download the page
    print('Dowloading page %s...' % (url))
    res = requests.get(url)
    res.raise_for_status()
    soup = bs4.BeautifulSoup(res.text, "html.parser")

    # find url of the comic image (&lt;div id ="comic"&gt;&lt;img src="........" 
    &lt;/div
    comicElem = soup.select('#comic img')
    if comicElem == []:
        print('Could not find any images')
    else:
       comicUrl = 'http:' + comicElem[0].get('src')

       #download the image
       print('Downloading image... %s' % (comicUrl))
       res = requests.get(comicUrl)
       try:
           res.raise_for_status()
       except requests.exceptions.MissingSchema as err:
           print(err)
           continue

        # save image to folder
        imageFile = open(os.path.join('xkcd',
        os.path.basename(comicUrl)), 'wb')
        for chunk in res.iter_content(1000000):
            imageFile.write(chunk)
        imageFile.close()

#get &lt;previous&gt; button url
prevLink = soup.select('a[rel="prev"]')[0]
url = 'http://xkcd.com' + prevLink.get('href')

print('Done')
</code></pre>

<p>What a my not doing? (I'm on python 3.5)
Thanks allot in advance...</p>

<p>if you don't care about the error (which i see as bad programming), just use a blank except statement that catches all exceptions.</p>

<pre><code>#download the image
print('Downloading image... %s' % (comicUrl))
try:
    res = requests.get(comicUrl) # moved inside the try block
    res.raise_for_status()
except:
    continue
</code></pre>

<p>but on the other hand if your except block isn't catching the exception then it's because the exception actually happens outside your try block, so move <code>requests.get</code> into the try block and the exception handling should work (that's if you still need it).</p>

python