K
K
kopelev20002019-01-06 16:54:44
Python
kopelev2000, 2019-01-06 16:54:44

TypeError: object of type 'NoneType' has no len()?

I can't figure out what's wrong, any help would be greatly appreciated!
Here is the error itself:
Traceback (most recent call last):
File "C:\Users\users\Desktop\����� ����� (3)\parser.py", line 24, in
main()
File "C:\Users\users\Desktop\����� ����� (3)\parser.py", line 18, in main
total_pages = get_total_pages(get_html(url))
File "C:\Users\ users\Desktop\����� ����� (3)\parser.py", line 8, in get_total_pages
soup = BeautifulSoup(html, 'html.parser')
File "C:\Users\users\AppData \Local\Programs\Python\Python37-32\lib\site-packages\bs4\__init__.py", line 245, in __init__
elif len(markup) <= 256 and (
TypeError: object of type 'NoneType' has no len ()
and here is the program code:

import requests
from bs4 import BeautifulSoup

def get_html(url):
    r = requests.get(url)

def get_total_pages(html):
    soup = BeautifulSoup(html, 'html.parser')
    pages = soup.find('div', class_="pagination-pages clearfix").find_all('a', class_="pagination-page")[-1].get('href')
    total_pages = pages.split('=')[1].split('&')[0]
    return int(total_pages)

def main():
    url = 'https://www.avito.ru/moskva/noutbuki?p=1&q=lenovo'
    BASE_URL = 'https://www.avito.ru/moskva/noutbuki?'
    PAGE_PART = 'p='
    QUERY_PART  ='&q=lenovo'
    total_pages = get_total_pages(get_html(url))
    for i in range(1, total_pages):
        url_gen = BASE_URL + PAGE_PART + str(i) + QUERY_PART
        print(url_gen)

if __name__ == '__main__':
    main()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2019-01-06
@kopelev2000

def get_html(url):
    r = requests.get(url)

There's an error. Search
PS It's more convenient to form urls through the urllib.parse module , and not like you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question