Ali Bahaari Ali Bahaari -3 years ago 288
Python Question

Cookies must be enabled to use GitHub in requests module python

I want to check some usernames & password for logging in to GitHub.
I Inspected & got that the first input name of github login is "login" & second one is "password" & "Form" uses "POST" method. I have written this code but when I want to check this code, It tells me (

Cookies must be enabled to use GitHub
)

allData = {
'login': x,
'password': y
}

siteRequest = requests.post('https://github.com/login', data=allData)
print(siteRequest.content, end='\n\n')


How should I enable cookies ?
Note : I know github has API but some sites don't, So I want to increase my ability & knowledge about this method ... Thanks!

Answer Source

GitHub's login page sets a cookie with a session ID on load, this session is used to keep you logged in.

To solve your problem simply create a requests session object, this will keep track of the cookies, then use the same session to post your login.

allData = {
    'login': x,
    'password': y
}

site_session = requests.Session()
_ = site_session.get('https://github.com/login')  # This is just to get the initial
                                                  # cookie in the session
siteRequest = site_session.post('https://github.com/login', data=allData)
print(siteRequest.content, end='\n\n')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download