error handling with BeautifulSoup when scraped url doesn't respond
<p>I'm totally noob to python so please forgive my mistake and lack of vocabulary. I'm trying to scrap some url with BeautifulSoup. My url are coming from a GA api call and some of them doesn't respond. </p>

<p>How do I build my script so that BeautifulSoup ignore the url that doesn't return anything ? </p>

<p>Here is my code :</p>

<pre><code>  if results:
    for row in results.get('rows'):
      rawdata.append(row[0])
  else:
    print 'No results found'

  urllist = [mystring + x for x in rawdata]

  for row in urllist[4:8]:  

    page = urllib2.urlopen(row)
    soup = BeautifulSoup(page, 'html.parser')


    name_box = soup.find(attrs={'class': 'nb-shares'})
    share = name_box.text.strip() 

    # save the data in tuple
    sharelist.append((row,share))

  print(sharelist)
</code></pre>

<p>I tried to use this :</p>

<pre><code>  except Exception: 
  pass
</code></pre>

<p>but I don't know where and got some syntax error. I've look at other questions, but cannot find any answers for me. </p>

<p>You may check the value of <code>name_box</code> variable - it would be <code>None</code> if nothing found:</p>

<pre><code>for row in urllist[4:8]:  
    page = urllib2.urlopen(row)
    soup = BeautifulSoup(page, 'html.parser')

    name_box = soup.find(attrs={'class': 'nb-shares'})
    if name_box is None:
        continue

    # ...
</code></pre>

python beautifulsoup