I'm trying to login to Gmail by just using requests and then proceed to watch youtube, do some searches etc. I don't want to use selenium or any other alternative to selenium as I find it bulky and inconvenient
I was researching how to do this and I came across some answers here and based my code of that. However these solutions are from a couple years back and I don't know if it still applies now and if it will work to the purpose I want it to.
class SessionGoogle:
def __init__(self, url_login, url_auth, login, pwd):
self.ses = requests.session()
login_html = self.ses.get(url_login)
soup_login = soup(login_html.content, "lxml").find('form').find_all('input')
my_dict = {}
for u in soup_login:
if u.has_attr('value'):
my_dict[u['name']] = u['value']
# override the inputs without login and pwd:
my_dict['Email'] = login
my_dict['Passwd'] = pwd
self.ses.post(url_auth, data=my_dict)
def get(self, URL):
return self.ses.get(URL).text
url_login = "https://accounts.google.com/ServiceLogin"
url_auth = "https://accounts.google.com/ServiceLoginAuth"
session = SessionGoogle(url_login, url_auth, "gmaillogint", "gmailpass")
r = session.get("http://plus.google.com")
p = session.get("http://google.com")
yt = session.get("https://youtube.com")
This code is taken from this post: Logging in to google using python?
The original post says that this can handle login on any google service page. Does this still apply today? If so can this be used to login to a Google (Gmail) account and watch youtube? Or will I have to use selenium?
Edit: I tried this code and it didn't result in any errors of any sort. However when I tried to print r.text or r.content I got a AttributeError: 'str' object has no attribute 'content'"error. This leads me to think that this is not working the way it should be.