I am trying to login to 9gag with python using requests lib. I inspected the post packets with firebug, found out that a csrftoken was used. Eventually I have managed to get the csrftoken and make the request with it.
But when I post the login parameters, I get a page with an error that says "disable cookies to login". I use a requests session to keep the login cookies.
My problem is that I can't get the links to nsfw posts if I don't login. Should I maybe tinker around with the HTTP Headers?
Here is my code:
import requests
import os
import re
from collections import OrderedDict
class Ninegaginstance():
def __init__(self, username, password):
self.s = requests.Session()
self.s.headers['User-Agent'] = 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3'
csrf = self.get_csrf(self.s.get('http://m.9gag.com/login').text)
payload = OrderedDict([('csrftoken', csrf), ('password', password),
('username', username)])
print(payload)
self.login(payload)
self.getpage()
def get_csrf(self, source):
match_csrf = re.compile('(name=\"csrftoken\" value=\")([\w\d]+)')
return match_csrf.search(source).group(2)
def login(self, credentials):
loginpage = self.s.post('http://m.9gag.com/login', params=credentials)
print(loginpage.status_code)
print(self.s.cookies)
with open('login.html', 'w') as loginpagefile:
print(loginpage.text, file=loginpagefile)
def getpage(self):
ninegagpage = self.s.get('http://m.9gag.com')
with open('9gagsource.html', 'w') as ninegagfile:
print(ninegagpage.text, file=ninegagfile)
newninegaginstance = Ninegaginstance('myusername', 'mypassword')