Home > Software engineering >  Trying to insert value in class method
Trying to insert value in class method

Time:02-10

I'm trying to pass an argument from the get_name() to post() and I'm not sure how to do this correctly

from bs4 import BeautifulSoup
import requests

webhook = 'my webhook'

class Item:
    def __init__(self, url):
        self.url = url
        self.page = requests.get(url)
        self.soup = BeautifulSoup(self.page.content, 'html.parser')

    def post(self):
        requests.post(webhook, json={'username' : 'scraper', 'content' : variable}) #how do i do this

    def get_name(self):
        name = self.soup.find('h2').get_text()
        post(name)

    def get_price(self):
        price = self.soup.find('span' {'class' : 'text-lg wait-for-i18n-format-render'})
        post(price)

CodePudding user response:

I guess, you want something like this:

from bs4 import BeautifulSoup
import requests

webhook = 'my webhook'

class Item:
    def __init__(self, url):
        self.url = url
        self.page = requests.get(url)
        self.soup = BeautifulSoup(self.page.content, 'html.parser')

    def post(self, data):
        requests.post(webhook, json={'username' : 'scraper', 'content' : data}) 

    def get_name(self):
        name = self.soup.find('h2').get_text()
        self.post(name)

CodePudding user response:

Two (maybe three) problems:

  1. post is not a function the enclosing scope; it is a class attribute of the class Item, and needs to be accessed as such from Item or an instance of Item.

  2. post needs to take an additional argument to be used as the value of the content key.

  3. Item.post should probably return the return value of requests.post, as the caller may want to that in order to, for example, check the result of the POST request.

Your class should look like

class Item:
    def __init__(self, url):
        self.url = url
        self.page = requests.get(url)
        self.soup = BeautifulSoup(self.page.content, 'html.parser')

    def post(self, variable):
        return requests.post(webhook, json={'username' : 'scraper', 'content' : variable})

    def get_name(self):
        name = self.soup.find('h2').get_text()
        self.post(name)

    def get_price(self):
        price = self.soup.find('span' {'class' : 'text-lg wait-for-i18n-format-render'})
        self.post(price)

CodePudding user response:

The self in the class means that any variable/attribute attached to self can be accessed by any function attached to self anywhere inside the class scope. This means that you are not passing arguments to parameters of a function but instead you use the self identifier.

So with your code the variable and function calls inside the functions are local to the function. To give the variable class scope, attach them to self. eg

    def get_name(self):
        name = self.soup.find('h2').get_text()
        post(name)

would become

    def get_name(self):
        self.name = self.soup.find('h2').get_text()
        self.post()

then in def post(self): you would reference self.name to process.

  • Related