0

I don't know why this isn't working

from bs4 import *
import time
import pandas as pd
import pickle
import html5lib
from requests_html import HTMLSession

s = HTMLSession()
url = "https://cryptoli.st/lists/fixed-supply"


def get_data(url):
    r = s.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')
    return soup
    
def get_next_page(soup):
    page = soup.find('ul', {'class': 'pager'})
    if not page.find('a', {'class': 'btn btn-default current disabled'}):
        url = 'https://cryptoli.st/lists/fixed-supply' + \
            str(page.find('li', {'class': 'paginate_button'}).find(
                'a')[{'class': 'btn btn-default next'}])
        return url
    else:
        return


get_data(url)
print(get_next_page(soup))

I have seen other scripts that return variables from one function to use in another but this keeps saying "soup" is not defined. Then if I make soup a global variable then I get the error that page is a Nonetype and I can't call the .find attribute off it. Any help would be appreciated.

5
  • 2
    You forgot to assign the result of get_data to data. Commented May 20, 2021 at 4:17
  • 1
    You haven't defined a variable named data. Commented May 20, 2021 at 4:18
  • 1
    This shouldn't be saying soup is not defined, it should say data is not defined. Commented May 20, 2021 at 4:18
  • 1
    Does this answer your question? python return - "name not defined" Commented May 20, 2021 at 4:37
  • Any ideas on why page.find is returning this nonetype error? or how to fix this? This is the error it's giving me: if not page.find('a', {'class': 'btn btn-default current disabled'}): AttributeError: 'NoneType' object has no attribute 'find' Commented May 20, 2021 at 4:41

3 Answers 3

2

Your last line for print(get_next_page(data)) is running the function get_next_page with the parameter data passed in. However, data is never defined, and so it passes in None. So then inside of get_next_page, it assigns soup = None. Then you are running everything else on None.

In the second-to-bottom line you need to do data = get_data(url), and then when you call get_next_page(data)), data will be equal to the soup that you returned from the first function.

Also, you probably need that s = HTMLSession() to either be inside of the get_url function, or pass it in like you do url

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I just edited it. When I run the get_next_page function soup should be passed in there. I changed the variable names
2

This is what you are doing.

def define_soup():
    soup = 'yummy'
    return soup 
def eat():
    return soup
define_soup() 
print(eat())

soup sure is defined in define_soup(), but it is local to that one function. No other function can use it. So assuming that because we have called define_soup() therefor we can use it in eat() is wrong. Instead you can make soup global or store the returned value of define_soup() in a variable.

using global

def define_soup():
    global soup
    soup = 'yummy'
    return soup 
def eat():
    return soup
define_soup() 
print(eat())

storing define_soup() output in var

def define_soup():
    soup = 'yummy'
    return soup 
def eat(soup):
    return soup
sp = define_soup() 
print(eat(sp))

1 Comment

No problem, glad to help.
0

get_data(url) function returns a variable but is not stored in anything. So you can do

data = get_data(url) print(get_next_page(data))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.