Y
Y
yk_vova2020-09-11 09:20:52
Python
yk_vova, 2020-09-11 09:20:52

Error in parser: AttributeError: 'NoneType' object has no attribute 'find'. How to fix?

I start the program, I reach the moment with entering the number, I enter it and an error appears:

Traceback (most recent call last):
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\telebot\util.py", line 62, in run
    task(*args, **kwargs)
  File "C:/Users/PycharmProjects/pythonProject5/main2.py", line 94, in geom
    parse()
  File "C:/Users/PycharmProjects/pythonProject5/main2.py", line 92, in parse
    get_content(html.text)
  File "C:/Users/PycharmProjects/pythonProject5/main2.py", line 86, in get_content
    imgg = div.find('img')['src']
AttributeError: 'NoneType' object has no attribute 'find'


"
code:
def geom(message):
    print(message.text)
    print(type(message.text))
    intmes = int(message.text)
    listnum = range(1, 851)
    if intmes in listnum:
        per = str(message)
        URL = 'https://megaresheba.ru/gdz/geometriya/8-klass/merzlyak/' + per + '-nomer'
        print(URL)
        HEADERS = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36',
            'accept': '*/*'}

        def get_html(url, params=None):
            r = requests.get(url, headers=HEADERS, params=params)
            return r

        imgg = ''

        def get_content(html):
            global imgg
            soup = BeautifulSoup(html, 'html.parser')
            div = soup.find('div', {'class': 'with-overtask'})
            imgg = div.find('img')['src']
            return imgg

        def parse():
            html = get_html(URL)
            if html.status_code == 200:
                get_content(html.text)
            return html
        parse()
        print(imgg)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Tikhonov, 2020-09-11
@yk_vova

Check if div is not None before calling div.find

I
IDzone-x, 2020-10-18
@IDzone-x

Well, because it can't find the element and returns None

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question