Flurry登录Requests.Session()Python 3 [英] Flurry Login Requests.Session() Python 3
问题描述
因此,我在此处之前已经回答了这个问题.但是,Flurry网站上的某些内容已更改,答案不再有效.
So I had this question answered 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)
上面的代码在上个月运行良好,然后在上周停止运行.对于我的一生,我无法弄清楚网站上发生了什么变化. ID名称和令牌都看起来正确,用户名和通行证没有更改.我不知所措.
The code above worked great for the last month and then it stopped working last week. For the life of me, I can't figure out what on the website has changed. ID Names and tokens all look correct, username and pass hasnt changed. Im at a loss.
如果我手动登录,则可以使用csvurl
很好地下载csv.
If I login manually, I can download the csv just fine using the csvurl
.
login.histroy
显示:
[<Response [302]>, <Response [302]>, <Response [302]>, <Response [302]>, <Response [303]>]
如果有人可以看看并弄清楚我要去哪里,我将不胜感激.
If anyone could take a look and figure out where I am going wrong, I would greatly appreciate it.
谢谢.
更新
因此,从新的登录地址开始,我看到帖子必须采用以下格式:
So from the new login address, I see the post needs to be in this format:
{"data":{"type":"session","id":"bd7d8dc1-4a86-4aed-a618-0b2765b03fb7","attributes":{"scopes":"","email":"myemail","password":"mypass","remember":"false"}}}
我不知道是他们如何生成ID的.谁能看一看?
What I can't figure out though is how they generated the id. Can anyone take a look?
推荐答案
您可以提供一个虚拟会话ID,它将使用新的ID登录. 邮递员拦截器有助于重定向.
You can offer up a dummy session id and it will log you in with a new one. Postman interceptor helped with the 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']
然后您可以在访问旧站点后获取csv数据:
And then you can grab your csv data after hitting the old site:
response = session.request('GET', 'https://dev.flurry.com/home.do')
data = session.request('GET', your_csv_url).text
这篇关于Flurry登录Requests.Session()Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!