-1

When I run it, I get an x ​​error after a certain time. I know I got this error because it's at the end of the page. How do I stop the program without receiving this error?

I tried

if newsContents == "NoneType":
        break

or

if newsContents == " ":
        break

My code is:

while i < 30:
    r = requests.get("blabla" + str(i))
    source = BeautifulSoup(r.content,"lxml")
    i=i+1
    newsContents = source.find("div", attrs={"class": "figCaption"})
    print(newsContents.text)
    print("#"*10)

print(newsContents.text) AttributeError: 'NoneType' object has no attribute 'text'

feyZ1g
  • 62
  • 8

1 Answers1

0

Try this if statement

if newsContents is None:
        break
Paolo Mossini
  • 1,064
  • 2
  • 15
  • 23