How to ignore an IndexError on Python

Jol*_*aha -2 python indexing

I'm trying to write a script that will go through a list of urls and scrape the web page connected to that url and save the contents to a text file. Unfortunately, a few random urls lead to a page that isn't formatted in the same way and that gets me an IndexError. How do I write a script that will just skip the IndexError and move onto the next URL? I tried the code below but just get syntax errors. Thank you so much in advance for your help.

from bs4 import BeautifulSoup, SoupStrainer
import urllib2
import io
import os
import re

urlfile = open("dailynewsurls.txt",'r') # read one line at a time until end of file
for url in urlfile:  
    try:

        page = urllib2.urlopen(url)
        pagecontent = page.read() # get a file-like object at this url

        soup = BeautifulSoup(pagecontent)

        title = soup.find_all('title')
        article = soup.find_all('article')

        title = str(title[0].get_text().encode('utf-8'))
    except IndexError:
        return None 
        article = str(article[0].get_text().encode('utf-8'))
    except IndexError:
        return None

       outfile = open(output_files_pathname + new_filename,'w')
       outfile.write(title)
       outfile.write("\n")
       outfile.write(article)
       outfile.close()

    print "%r added as a text file" % title

print "All done." 
Run Code Online (Sandbox Code Playgroud)

The error I get is: File "dailynews.py", line 39 except IndexError: ^ SyntaxError: invalid syntax

Tot*_*tem 6

you would do something like:

try:
    # the code that can cause the error
except IndexError: # catch the error
    pass # pass will basically ignore it
         # and execution will continue on to whatever comes
         # after the try/except block
Run Code Online (Sandbox Code Playgroud)

如果您处于循环中,则可以使用continue代替pass. continue将立即跳转到循环的下一次迭代,无论它从中跳转的迭代中是否有更多代码要执行。sys.exit(0)将结束程序。