如何使用python登录并下载.csv文件?

时间:2018-11-16 15:03:45

标签: python python-requests

我正尝试登录以从此URL下载.csv文件 https://www.mapmyfitness.com/workout/export/csv。该URL将页面重定向到该URL: https://www.mapmyfitness.com/auth/login?next=/workout/export/csv

我尝试实现以下代码,但出现此错误:

username = 'email'
password = 'password'

url1 = 'https://www.mapmyfitness.com/workout/export/csv'
url2 = 'https://www.mapmyfitness.com/auth/login?next=/workout/export/csv'

payload = {'email': username, 'password': password}
requests.post(url2, data=payload)

with requests.Session() as s:
    p = s.post(url2, data=payload)
    print(p.text)

    r = s.get(url2)
    # print(r.text)

{"error":"validation/username,password","error_description":"Post body failed validation: {\"username\":\"validation/string\",\"password\":\"validation/string\"}"}

我已经尝试了许多其他方法,但这是我最接近该页面的方法,而不仅仅是吐出登录页面上的HTML信息。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您的问题是,您试图将登录页面视为API调用,但它们是不同的动物。您将需要让python(硒可以做到这一点)实际填写实际的输入字段并解析结果。这似乎很麻烦,对网页的任何更改都会破坏这一点。

似乎他们的软件具有API,您应该实际看一下:

https://developer.underarmour.com/

单击从How to press login button in selenium python 3拉出硒的按钮

xpath:

elem = driver.find_element_by_xpath("//input[@class='userid-button'][@type='submit']")
elem.click()

css:

elem = driver.find_element_by_css_selector("input.userid-button[type=submit]")
elem.click()