I've created a script in Python to log into stackoverflow.com using credentials and fetch the profilename once logged in. I've tried to do it using class. I created the methods within that class in such a way so that they work like chain. Should I stick to this design or there is anything better I can pursue? Whatever it is I would like this get_profile() method to be seperated like how it is now.
from bs4 import BeautifulSoup
import requests
class StackOverflowBot(object):
login_url = "https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2f"
def __init__(self,session,username,password):
self.session = session
self.username = username
self.password = password
self.login(self.session,self.username,self.password)
def login(self,session,username,password):
session.headers['User-Agent'] = 'Mozilla/5.0'
req = session.get(self.login_url)
soup = BeautifulSoup(req.text, "lxml")
payload = {
"fkey": soup.select_one("[name='fkey']")["value"],
"email": username,
"password": password,
}
req = session.post(self.login_url,data=payload)
return self.get_profile(req.text)
def get_profile(self,htmlcontent):
soup = BeautifulSoup(htmlcontent,"lxml")
item = soup.select_one("[class^='gravatar-wrapper-']").get('title')
print(item)
if __name__ == '__main__':
with requests.Session() as session:
StackOverflowBot(session,"username","password")
1 Answer 1
IMO the answer highly depends on how you are planning to extend the functionality of your class. If its only function is fetching the username, it's probably better to transform it into a function. Class is an overkill.
A few thoughts if you're going to expand it:
- You can take a look at the Stack Exchange API and see if it matches your needs if you haven't already.
- You can cache user data in the class fields.
- If you don't care too much about making one extra request, you can make one when you call
get_profile. For this, you'll probably need to store the session credentials. - You can try using
HEADmethod instead ofGETwhen you don't need the body (dunno though if the site will handle it as intended).
Also, in Python 3 there is no reason to extend object for new classes.
You must log in to answer this question.
Explore related questions
See similar questions with these tags.