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.

Justin Benfit
  • 423
  • 3
  • 11
  • 2
    You forgot to assign the result of `get_data` to `data`. – Barmar May 20 '21 at 04:17
  • 1
    You haven't defined a variable named `data`. – Selcuk May 20 '21 at 04:18
  • 1
    This shouldn't be saying `soup` is not defined, it should say `data` is not defined. – Barmar May 20 '21 at 04:18
  • 1
    Does this answer your question? [python return - "name not defined"](https://stackoverflow.com/questions/35242714/python-return-name-not-defined) – outis May 20 '21 at 04: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' – Justin Benfit May 20 '21 at 04:41

3 Answers3

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

scotscotmcc
  • 2,719
  • 1
  • 6
  • 29
  • Sorry I just edited it. When I run the get_next_page function soup should be passed in there. I changed the variable names – Justin Benfit May 20 '21 at 04:27
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))
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44
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))