4

First of all, I know there are a bunch of similar questions but unfortunately none of them work for me. I am a relative noob in python and simple explanations and answers would be greatly appreciated.

I need to log into a site programmatically using python. I am attempting to do this using requests. I've watched YouTube videos on the subject and looked at various questions and and answers but it just doesn't work for me.

The following code is as close as I have gotten to achieving my goal. The IDE I am using is Spyder 3.1.2 with python 3.6.0. My output is coming up as [] as shown below my code. I have tried the same method with other sites and the output is always the same. I do not know what this means however. And how would I know if the code has worked?

import requests
from lxml import html

USERNAME = "username"
PASSWORD = "password"

LOGIN_URL = "https://bitbucket.org/account/signin/?next=/"
URL = "https://bitbucket.org/"

def main():
    session_requests = requests.session()

    # Get login csrf token
    result = session_requests.get(LOGIN_URL)
    tree = html.fromstring(result.text)
    authenticity_token = list(set(tree.xpath("//input[@name='csrfmiddlewaretoken']/@value")))[0]

    # Create payload
    payload = {
        "username": USERNAME, 
        "password": PASSWORD, 
        "csrfmiddlewaretoken": authenticity_token
    }

    # Perform login
    result = session_requests.post(LOGIN_URL, data = payload, headers = dict(referer = LOGIN_URL))

    # Scrape url
    result = session_requests.get(URL, headers = dict(referer = URL))
    tree = html.fromstring(result.content)
    bucket_names = tree.xpath("//div[@class='repo-list--repo']/a/text()")

    print(bucket_names)

if __name__ == '__main__':
    main()

runfile('C:/Users/Thomas/untitled6.py', wdir='C:/Users/Thomas') []

Thank you in advance.

chickencreature.

Abdulrahman Bres
  • 2,603
  • 1
  • 20
  • 39

1 Answers1

1

try this.

result = requests.get(LOGIN_URL, auth=(USERNAME,PASSWORD))

Check out the answers of these similar questions This and This.
Here is the documentation of authentication using requests module

Community
  • 1
  • 1
MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59