Exception handling when the input link doesn't have the appropriate form

unreneise

New Member
for instance, i have a list of links like this:\[code\]linklists = ['www.right1.com', www.right2.com', 'www.wrong.com', 'www.right3.com']\[/code\]and the form of each right1,right2 and right3's html is:\[code\]<html><p>hi</p><strong>hello</strong></html>\[/code\]and the form of www.wrong.com html is(actual html is much more complicated):\[code\]<html><p>hi</p></html>\[/code\]and i'm using a code like this:\[code\]from BeautifulSoup import BeautifulSoupstronglist=[]for httplink in linklists: url = httplink page = urllib2.urlopen(url) html = page.read() soup = BeautifulSoup(html) findstrong = soup.findAll("strong") findstrong = str(findstrong) findstrong = re.sub(r'\[|\]|\s*<[^>]*>\s*', '', findstrong) #remove tag stronglist.append(findstrong)\[/code\]what i want to do is:[*]get through html links from the list \[code\]'linklists'\[/code\][*]find data between \[code\]<strong>\[/code\][*]add them to list \[code\]'stronglist'\[/code\]but the problem is:there is a wrong link (\[code\]www.wrong.com\[/code\]) that has no .then the code says error...what i want is an exception handling(or something else) that if the link doesn't have 'strong' field(it has error), i want the code to add the string 'null' to the stronglist since it can't get data from the link.i have been using 'if to solve this but it's a bit hard for meany suggestions?
 
Back
Top