#StackBounty: #python #python-3.x #object-oriented #web-scraping #stackexchange Scraper to grab a Stack Overflow profile name

Bounty: 50

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:

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.