Skip to content

Instantly share code, notes, and snippets.

@vaniakov
Created April 24, 2016 20:11
Show Gist options
  • Save vaniakov/2ef71a59e10bcba7b1c357fd0257ec06 to your computer and use it in GitHub Desktop.
Save vaniakov/2ef71a59e10bcba7b1c357fd0257ec06 to your computer and use it in GitHub Desktop.
import requests
from lxml import html
USERNAME = "<USERNAME>"
PASSWORD = "<PASSWORD>"
LOGIN_URL = "https://bitbucket.org/account/signin/?next=/"
URL = "https://bitbucket.org/dashboard/overview"
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_elems = tree.findall(".//span[@class='repo-name']")
bucket_names = [bucket_elem.text_content().replace("\n", "").strip() for bucket_elem in bucket_elems]
print bucket_names
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment