Python – I received AttributeError : ‘NoneType’ object has no attribute ‘find’

I received AttributeError : ‘NoneType’ object has no attribute ‘find’… here is a solution to the problem.

I received AttributeError : ‘NoneType’ object has no attribute ‘find’

Hello, here is my code to retrieve the first theme in the ycombinator website. When I run the code, I get –

AttributeError: 'NoneType' object has no attribute 'find'for the line 
level2= data.level1.find('table',attrs = {'id':'hnmain'})

Topics are deeply embedded in various tabs, which is why I proceed as follows. I did this just to practice, so I knew it might not be the best way to code on my first day, and I just wanted to know how to overcome the mistakes.

import requests
from bs4 import BeautifulSoup
response1= requests.get('https://news.ycombinator.com/')
response = response1.text

data = BeautifulSoup(response,"html.parser")

level1= data.body.find('centre')
level2= data.level1.find('table',attrs = {'id':'hnmain'})
level3= data.level2.find('tbody')
level4= data.level3.find('tr')
level5= data.level4.find('td')
level6= data.level5.find('table.itemlist')
level7= data.level6.find('tbody')
level8= data.level7.find('tr#15426209.athing')
level9= data.level8.find('td.title')
level10= data.level9.find('a.storylink')
print(level10.text)

Solution

I think you’re getting the error because of the data.body section. I’ve never seen it done that.

Here is a modified version of your code:

import requests
from bs4 import BeautifulSoup

r = requests.get('https://news.ycombinator.com')

soup = BeautifulSoup(r.text, 'lxml')

# print soup.prettify()

stories = []

for a in soup.find_all('a', attrs={'class': 'storylink'}):
    stories.append([a.text, a['href']])

print stories[0]

[u'Using Binary Diffing to Discover Windows Kernel Memory Disclosure Bugs', 'https://googleprojectzero.blogspot.com/2017/10/using-binary-diffing-to-discover.html']

I’ve commented out soup.pretify(), but you can uncomment and see what it does – it shows you the page’s source code in a well-organized way.

Related Problems and Solutions