我正在尝试使用请求库通过 python 获取 SalesForce 报告。我能够成功登录。我用 python 得到的输出不包含任何文本正文。如何从 python 输出中提取文本。
from simple_salesforce import Salesforce
import requests
import pandas as pd
sf = Salesforce(username='',
password='',
security_token='')
export_url = 'https://gkg-mfsa.lightning.force.com/lightning/r/Report/00O9N000000JwK2UAK/?export=1&enc=UTF-8&cf=csv'
session = requests.Session()
response = session.get(export_url,
headers=sf.headers,
cookies={'sid': sf.session_id})
download_report = response.content.decode('utf-8')
print(download_report)
输出
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<script>
function redirectOnLoad() {
if (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNavigator.handleRedirect('https://gkg-mfsa.my.salesforce.com?ec=302&startURL=%2Fvisualforce%2Fsession%3Furl%3Dhttps%253A%252F%252Fgkg-mfsa.lightning.force.com%252Flightning%252Fr%252FReport%252F00O9N000000JwK2UAK%252F%253Fexport%253D1%2526enc%253DUTF-8%2526cf%253Dcsv'); } else
if (window.location.replace){
window.location.replace('https://gkg-mfsa.my.salesforce.com?ec=302&startURL=%2Fvisualforce%2Fsession%3Furl%3Dhttps%253A%252F%252Fgkg-mfsa.lightning.force.com%252Flightning%252Fr%252FReport%252F00O9N000000JwK2UAK%252F%253Fexport%253D1%2526enc%253DUTF-8%2526cf%253Dcsv');
} else {
window.location.href ='https://gkg-mfsa.my.salesforce.com?ec=302&startURL=%2Fvisualforce%2Fsession%3Furl%3Dhttps%253A%252F%252Fgkg-mfsa.lightning.force.com%252Flightning%252Fr%252FReport%252F00O9N000000JwK2UAK%252F%253Fexport%253D1%2526enc%253DUTF-8%2526cf%253Dcsv';
}
}
redirectOnLoad();
</script>
</head>
</html>
<!-- Body events -->
<script type="text/javascript">function bodyOnLoad(){if(window.PreferenceBits){window.PreferenceBits.prototype.csrfToken="null";};}function bodyOnBeforeUnload(){}function bodyOnFocus(){}function bodyOnUnload(){}</script>
</body>
</html>
<!--
...................................................................................................
...................................................................................................
...................................................................................................
...................................................................................................
-->
如何从输出中获取文本部分?
编辑:
我还尝试了 simple_salesforce 库方法,特别是使用 sf.restful 来获取报告。这给了我一个无效的会话 ID 错误,我还发布了一个关于此问题的问题,你可以在这里找到它
此“对我有用”(组织尚未启用“增强域”),请输入您的报告 ID
from simple_salesforce import Salesforce
import requests
from io import StringIO
sf = Salesforce(username='[email protected]',
password='hunter2',
security_token='')
print(sf.sf_instance)
print(sf.session_id)
export_url = export_url = 'https://' + sf.sf_instance + '/' + '00O5J000000y5LVUAY?isdtp=p1&export=1&enc=UTF-8&xf=csv'
session = requests.Session()
response = session.get(export_url,
headers=sf.headers,
cookies={'sid': sf.session_id})
download_report = response.content.decode('utf-8')
# print(download_report)
data = StringIO(download_report)
df = pd.read_csv(data)
df