I am a brand new to coding (a couple weeks) and im writing some code to webscrape some HTML. The majority of my questions are more structure based about classes and some of the explanations I don't really understand, so if possible try and break it down for me. I am using beautifulsoup to webscrape
I have 2 questions that build on one another:
I am still unsure of when to use
self
and how to use it, when the point of this class is to really just organize functions, and also that I'd like to practice using classes in general. The confusing part is that it runs without the self argument, but I remember from many other sources I've read that it requires that argument. I suspect it could be a static function but when I try to set that up I don't really understand what I am doing and I get errors. If someone could help me fill in the blanks of my knowledge that would be great.From what I have read about classes I believe I can use
__init__
to define a few variables or processes that all the functions in the class can reference. In this instance I am trying to passpage
andsoup
to the three functions insideclass UND
as these never change in the program and I hate to repeat the lines of code for every function. The issue is if I try to set up this__init__
to pass the variables, it does not work because I haven't usedself
. I'm just super confused.
The first box of code is what I am using right now and it works! But I feel I need to address the issues outlined above.
The second box of code is my attempt to use __init__
, but it has errors that it is missing 'self'
Thank you to all who may (or may not!) help!
class UND:
def getFlightCategory(): # Takes the appropriate html text and sets it to a variable
page = requests.get("http://sof.aero.und.edu")
soup = BeautifulSoup(page.content, "html.parser")
flightCategoryClass = soup.find(class_="auto-style1b")
return flightCategoryClass.get_text()
def getRestrictions(): # Takes the appropriate html text and sets it to a variable
page = requests.get("http://sof.aero.und.edu")
soup = BeautifulSoup(page.content, "html.parser")
flightRestrictionsClass = soup.find(class_="auto-style4")
return flightRestrictionsClass.get_text()
def getInfo():
return UND.getFlightCategory(), UND.getRestrictions()
class UND:
def __init__(self):
page = requests.get("http://sof.aero.und.edu")
soup = BeautifulSoup(page.content, "html.parser")
def getFlightCategory(self): # Takes the appropriate html text and sets it to a variable
flightCategoryClass = soup.find(class_="auto-style1b")
return flightCategoryClass.get_text()
def getRestrictions(self): # Takes the appropriate html text and sets it to a variable
flightRestrictionsClass = soup.find(class_="auto-style4")
return flightRestrictionsClass.get_text()
def getInfo():
return UND.getFlightCategory(), UND.getRestrictions()