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

    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')

if __name__ == '__main__':
    with requests.Session() as session:

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:

  1. You can take a look at the Stack Exchange API and see if it matches your needs if you haven't already.
  2. You can cache user data in the class fields.
  3. 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.
  4. You can try using HEAD method instead of GET when 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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.