Search code examples
pythonclassprogram-entry-point

How do I call a class for use in my main function


I want to use a class in my main function but don't exactly how to do it. Here's what I have tried so far. The following code is only for demonstration purposes.

Code has been edited to what @furas has suggested.

import requests
from bs4 import BeautifulSoup
from class.embed import Embed


def request(r):
    s = requests.Session()
    r = s.get(url)
    return r

def data(r):
    soup = BeautifulSoup(r.txt, 'lxml')
    title = soup.select('.ac-ln-title-comingsoon')
    return title

def main():
    url = 'https://www.apple.com/macbook-pro-16/'
    old_title = None
    while True:
        r = request(url)
        title = data(r)
        if title != old_title:
            url = 'https://www.apple.com/macbook-pro-16/specs/'
            embed_class = Embed(url)
            print(price, processor)
        else:
            print('Lorem ipsum')

if __name__ == "__main__":
    main()

This is my class:

class Embed:
    def __init__(self, url):
        self.r = request(url)
    def content(r):
        soup = BeautifulSoup(self.r.text, 'lxml')
        price = soup.select('.column  large-6').get_text()
        processor = soup.select('.techspecs-column').get_text()

Solution

  • First you have to use self as first argument in all methods. When you will run

    embed_class.content()
    

    then Python will runs

    Embed.content(embed_class)
    

    so it will assign instance embed_class to self

    Second you should use self. to have access to variables from one method in another method.

    Method should also use return so you can use its values outside method.


    BTW:

    select() gives list (even if it find only one element or it doesn't find any element) so you can't use select().get_text() but you have to use get_text() on every element separatelly using for-loop or list comprehension. ie.

     price = [item.get_text(strip=True) for item in price]
    

    Page has two prices and two descriptions for processor so you may need for-loop to display it in more readable way. I display it as list.

    I skiped while-loop because it was useless for me. I also changed classes in select() to get only some part of page.


    import requests
    from bs4 import BeautifulSoup
    
    class Embed:
        def __init__(self, url):
            self.r = request(url)
    
        def content(self):
            soup = BeautifulSoup(self.r.text, 'lxml')
    
            price = soup.select('.section-price .column.large-6')
            price = [item.get_text(strip=True) for item in price]
    
            processor = soup.select('.section-processor')
            processor = [item.get_text(strip=True) for item in processor]
    
            return price, processor
    
    
    def request(url):
        s = requests.Session()
        r = s.get(url)
        return r
    
    def data(r):
        soup = BeautifulSoup(r.text, 'lxml')
        title = soup.select('.ac-ln-title-comingsoon')
        title = title[0].get_text()
        return title
    
    def main():
        url = 'https://www.apple.com/macbook-pro-16/'
        old_title = None
    
        r = request(url)
        title = data(r)
        print(title)
    
        if title != old_title:
            url = 'https://www.apple.com/macbook-pro-16/specs/'
            embed_class = Embed(url) # it runs only `__init__()`
            price, processor = embed_class.content()
            print(price, processor)
        else:
            print('Lorem ipsum')
    
    if __name__ == "__main__":
        main()