Skip to content

Instantly share code, notes, and snippets.

@tonosaman
Created June 7, 2012 23:23
Show Gist options
  • Save tonosaman/2892315 to your computer and use it in GitHub Desktop.
Save tonosaman/2892315 to your computer and use it in GitHub Desktop.
SalesForce Report CSV downloader
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from mechanize import Browser
from urllib import quote
import re
def fetch_sf_csv(report_id):
login_uri = "https://login.salesforce.com/"
username = "example@salesforce.com"
password = "<your-password>"
b = Browser()
b.set_handle_robots(False)
b.open(login_uri + '/?un=' + username + '&pw=' + password + '&startURL=' + quote(report_id) + '?export=1&enc=UTF-8&xf=csv')
link = b.find_link(url_regex=re.compile("^https://[^/]*salesforce.com/" + report_id))
return b.follow_link(link).read()
def fetch_sf_opportunities_csv():
return fetch_sf_csv("00OE0000000Gu0p")
if __name__ == "__main__":
print fetch_sf_opportunities_csv()
@sckaiser
Copy link

sckaiser commented Nov 5, 2015

Thanks for posting! Extremely useful.

I believe you have an extra / in the URI
https://login.salesforce.com//?un=
vs.
https://login.salesforce.com/?un=
After taking trailing . from the login_uri, I was able to use the script.
Also, I had to use Python 2, and urlllib2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment