0

我使用以下 curl 命令使用 cookiejar 文件获取数据:

$ curl -sk -X 'POST' -d "username=name@domain.com&password=mypassword" -c app.cookie-jar -k https://website.com/auth/authenticate
$ curl -sk -X 'GET' -H 'Accept: text/csv' -b app.cookie-jar https://website.com/api/systems > out.csv

有人可以帮助实现相同的python脚本吗

4

1 回答 1

0

使用Requests设置会话以跟踪 cookie:

import requests

with requests.Session() as s:
    resp = s.post('https://website.com/auth/authenticate', data='username=name@domain.com&password=mypassword')
    resp.raise_for_status()
    resp = s.get('https://website.com/api/systems', headers={'Accept': 'text/csv'})
    resp.raise_for_status()
    with open('out.csv', 'wb') as outf:
        outf.write(resp.content)
于 2020-02-20T13:58:26.433 回答