One option is to put the offending loop construct into a try, then handle the exception that appears when the interator returns None:
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj = BeautifulSoup(html,"html.parser")
try:
for child in bsObj.find("table",{"id":"giftlist"}).children:
print(child)
except AttributeError:
# do what you want to do when bsObj.find() returns None
Or you could check the resulting list for None before entering the loop:
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj = BeautifulSoup(html,"html.parser")
result = bsObj.find("table",{"id":"giftlist"})
if result:
for child in result.children:
print(child)
else:
# do what you want to do when bsObj.find() returns None