Entrance for towing. Session () Python 3

So, I asked this question before here . However, something on the Flurry website has changed and the answer no longer works.

from bs4 import BeautifulSoup import requests loginurl = "https://dev.flurry.com/secure/loginAction.do" csvurl = "https://dev.flurry.com/eventdata/.../..." #URL to get CSV data = {'loginEmail': 'user', 'loginPassword': 'pass'} with requests.Session() as session: session.headers.update({ "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36"}) soup = BeautifulSoup(session.get(loginurl).content) name = soup.select_one("input[name=struts.token.name]")["value"] data["struts.token.name"] = name data[name] = soup.select_one("input[name={}]".format(name))["value"] login = session.post(loginurl, data=data) getcsv = session.get(csvurl) 

This code worked fine for the last month, and then it stopped working last week. For life, I cannot understand what has changed on the website. Name and token identifiers look correct, username and password are not changed. Im at a loss.

If I log in manually, I can download csv just fine using csvurl .

login.histroy shows:

 [<Response [302]>, <Response [302]>, <Response [302]>, <Response [302]>, <Response [303]>] 

If anyone can take a look and find out where I'm wrong, I would really appreciate it.

Thanks.

UPDATE

So, from the new login address, I see that the message should be in this format:

 {"data":{"type":"session","id":"bd7d8dc1-4a86-4aed-a618-0b2765b03fb7","attributes":{"scopes":"","email":"myemail","password":"mypass","remember":"false"}}} 

What I cannot understand is how they generated the identifier. Can anyone take a look?

+5
source share
3 answers

You can offer a dummy session id and it will log in with a new one. Postman interceptor helped with redirects.

 import requests import json def login(email, password, session, session_id=None): """ Authenticate with flurry.com, start a fresh session if no session id is provided. """ auth_url = 'https://auth.flurry.com/auth/v1/session' login_url = 'https://login.flurry.com' auth_method = 'application/vnd.api+json' if session_id is None: session_id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' response = session.request('OPTIONS', auth_url, data='') headers = response.headers headers.update({'origin': login_url, 'referer': login_url, 'accept': auth_method, 'content-type': auth_method}) data = {'data': {'type': 'session', 'id': session_id, 'attributes': { 'scopes': '', 'email': email, 'password': password, 'remember': 'false'}}} payload = json.dumps(data) response = session.request('POST', auth_url, data=payload, headers=headers) return response email, password = 'your-email', 'your-password' session = requests.Session() response = login(email, password, session) # session_id = response.json()['data']['id'] 

And then you can capture the csv data after getting to the old site:

 response = session.request('GET', 'https://dev.flurry.com/home.do') data = session.request('GET', your_csv_url).text 
+1
source

Now they have a new design and a new login page to which they redirect you - that’s why you see the status codes 302 and 303. The login process and the logic behind it, URLs, links to CSV files - now all -other, and you need to "override" / "rename" it.

+1
source

you can use uuid lib to generate uuid for the session id, to use the old interface, you need to execute the request https://dev.flurry.com/home.do?isFirstPostLogin=true , now you can get csv. (url_get variable)

 id = uuid.uuid4() payload = {"data": {"type":"session", "id": str(id), "attributes":{ "scopes":"", "email": username, "password": password, "remember":"false"} } } with session() as api: headers = { 'Origin': 'https://login.flurry.com', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36', 'Content-Type': 'application/vnd.api+json', 'Accept': 'application/vnd.api+json', 'Connection': 'keep-alive', } req = api.post('https://auth.flurry.com/auth/v1/session', data=json.dumps(payload), headers=headers) if req.status_code == 201: api.get('https://dev.flurry.com/home.do?isFirstPostLogin=true') return api.get(url_get).content.encode('ascii', 'ignore') else: raise Exception('Login failed') 
0
source

All Articles