I'm new to Python, working on my first project; challenging myself to incorporate OOP into it. I've pre-written it procedurally and it works fine, however looking for best practices to re-work it into OOP.
My question for you: Is it better to call Beautiful soup within a class, so that each object is a self-contained entity, as I've done below in the discography function? (I've got multiple classes, each would call Beautiful Soup accordingly) Or, would it make more sense to call it globally; in which case how would I pass all the necessary variables into each of the classes?
Below; an example class I'm working on. Input a band name, and returns a dict of all their albums with URLs to the album's wikipedia page.
import urllib.request
from bs4 import BeautifulSoup
class Band:
def __init__(self, name):
self.name = name
def discography(self):
url = 'https://en.wikipedia.org/wiki/Special:Search?search=%s_discography' % self.name
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page, 'html.parser')
table = soup.find('table', attrs={'class': 'wikitable plainrowheaders'})
titles = table.findAll(attrs={'scope': 'row'})
def getAlbumTitles(url):
albumTitles = [title.text for title in titles]
return albumTitles
def getDiscogLinks(url):
album_href = [row.findAll('a') for row in titles]
clean_links = []
for i in album_href:
for href in i:
if href.parent.name == 'i':
clean_links.append('https://en.wikipedia.org' + href.get('href'))
return clean_links
return dict(zip(getAlbumTitles(url), getDiscogLinks(url)))
name = input('Enter a band name: ')
artist = Band(name)
print(artist.discography())
#beatles = Band('beatles')
#print(beatles.discography())