Search code examples
pythonrweb-scrapingpython-requestshttr

Convert R GET method into Python


I am new to Python; currently, I need to convert the below R code to Python, I know I need to use requests library; however, I don`t know how to convert the authenticate(cibc_username, cibc_password,"gssnegotiate") into Python. So, could anyone help me to resolve it?

The R code below:

hr_page = GET(url, user_agent("""Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko\)
Chrome/70.0.3538.77 Safari/537.36"""), authenticate(cibc_username, cibc_password,"gssnegotiate"), timeout(20000))

What I write:

headers = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"}
data = {'username':cibc_username, 'password':cibc_password}
hr_page = requests.get(url, headers = headers, data = data, timeout = 20000)

Solution

  • If the auth method is indeed gssapi, you may need to use HTTPNegotiateAuth from requests_negotiate:

    import requests
    from requests_negotiate import HTTPNegotiateAuth
    
    headers = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"}
    response = requests.get(url, headers = headers, timeout = 20000, auth=HTTPNegotiateAuth(cibc_username,cibc_password))
    

    More info here