Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use requests module in Python to log in to Barclays premier league fantasy football?

I'm trying to write a Python script to let me log in to my fantasy football account at https://fantasy.premierleague.com/, but something is not quite right with my log in. When I login through my browser and check the details using Chrome developer tools, I find that the Request URL is https://users.premierleague.com/accounts/login/ and the form data sent is:

csrfmiddlewaretoken:[My token]
login:[My username]
password:[My password]
app:plfpl-web
redirect_uri:https://fantasy.premierleague.com/a/login

There are also a number of Request headers:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:185
Content-Type:application/x-www-form-urlencoded
Cookie:[My cookies]
Host:users.premierleague.com
Origin:https://fantasy.premierleague.com
Referer:https://fantasy.premierleague.com/
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36

So I've written a short Python script using the request library to try to log in and navigate to a page as follows:

import requests
with requests.Session() as session:
url_home = 'https://fantasy.premierleague.com/'
html_home = session.get(url_home)
csrftoken = session.cookies['csrftoken']
values = {
    'csrfmiddlewaretoken': csrftoken,
    'login': <My username>,
    'password': <My password>,
    'app': 'plfpl-web',
    'redirect_uri': 'https://fantasy.premierleague.com/a/login'
}
head = {
    'Host':'users.premierleague.com',
    'Referer': 'https://fantasy.premierleague.com/',
}
session.post('https://users.premierleague.com/accounts/login/', 
             data = values, headers = head)
url_transfers = 'https://fantasy.premierleague.com/a/squad/transfers'
html_transfers = session.get(url_transfers)
print(html_transfers.content)

On printing out the content of my post request, I get a HTML response code 500 error with:

b'\n<html>\n<head>\n<title>Fastly error: unknown domain users.premierleague.com</title>\n</head>\n<body>\nFastly error: unknown domain: users.premierleague.com. Please check that this domain has been added to a service.</body></html>'  

If I remove the 'host' from my head dict, I get a HTML response code 405 error with:

b''

I've tried including various combinations of the Request headers in my head dict and nothing seems to work.

like image 643
Michael Andrew Bentley Avatar asked Aug 07 '16 23:08

Michael Andrew Bentley


People also ask

Does FPL have an API?

With fpl you can easily use the Fantasy Premier League API in all your Python scripts, exactly how you expect it to work.

How do I use FPL API?

Just go to https://fantasy.premierleague.com, sign in with your account, go to 'Pick Team' then 'View Gameweek history'. You can find your id in the URL, right before the '/history'. Update: If you have been authenticated, you can simply hit https://fantasy.premierleague.com/api/me/ to get your profile information.


1 Answers

The following worked for me. I simply removed headers = head

session.post('https://users.premierleague.com/accounts/login/', 
             data = values)

I think you are trying to pick your team programmatically, like me. Your code got me started thanks.

like image 143
TKerr Avatar answered Sep 20 '22 03:09

TKerr