Cookie Monster
Cookie Monster

Reputation: 59

name 'get_pages_count' is not defined

I am writing a parser but I have a problem. I understand that you can find many similar questions on the Internet, but they did not suit me. Therefore, I ask for help from you.I have little experience, so this question may not be very correct.

Code:

import requests
from bs4 import BeautifulSoup

URL = 'https://stopgame.ru/topgames'
HEADERS = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0',
'accept': '*/*'}
HOST = 'https://stopgame.ru'


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

def get_pages(html):
    soup = BeautifulSoup(html, 'html.parser')
    pagination = soup.find_all('a', class_='page')
    if pagination:
        return int(pagination[1].get.text())
    else:
        return 1



def get_content(html):
    soup = BeautifulSoup(html, 'html.parser')
    items = soup.find_all('div', class_="lent-brief")

    games = []
    for item in items:
        games.append({
            "title":  item.find("div", class_="title lent-title").get_text(strip=True),
            "date": item.find("div", class_="game-date").get_text(strip=True),
            "ganre": item.find("div", class_="game-genre").get_text(strip=True),




        })
        print(games)
    print(len(games))
    return games
def parse():
    html = get_html(URL)

    if html.status_code == 200:
        pages_count = get_pages_count(html.text)
        print(pages_count)
    else:
        print('Error')


parse()

Error:

File "D:/Python/parser1.py", line 45, in parse
        pages_count = get_pages_count(html.text) 
    NameError: name 'get_pages_count' is not defined

Upvotes: 0

Views: 188

Answers (2)

KunduK
KunduK

Reputation: 33384

In this below function the method you have called is wrong. Instead of this pagination[1].get.text() it should be pagination[1].get_text() or pagination[1].text

Code:

def get_pages(html):
    soup = BeautifulSoup(html, 'html.parser')
    pagination = soup.find_all('a', class_='page')
    if pagination:
        return int(pagination[1].get_text())
    else:
        return 1

OR

def get_pages(html):
    soup = BeautifulSoup(html, 'html.parser')
    pagination = soup.find_all('a', class_='page')
    if pagination:
        return int(pagination[1].text)
    else:
        return 1

Upvotes: 0

MatsLindh
MatsLindh

Reputation: 52892

Your function is named get_pages, but you're calling get_pages_count:

def get_pages(html):

.. but when attempting to call it:

pages_count = get_pages_count(html.text)

.. the call should be:

pages_count = get_pages(html.text)

Upvotes: 1

Related Questions