0

I'm trying to download a CSV file from a URL & save it to file on my hard drive. I'm trying to use the following code on Python 2.7 but getting errors. The CSV is located on a SharePoint site.

import urllib
import csv

url = 'https://office.com/sites/20Reporting/Lists/Reports/476%20-%2050%20DaySDShrink%20Report/476-%2030%20DaySDShrink.csv'
csv = urllib.urlopen(url).read() # returns type 'str'
with open('C:\Users\Documents\DPAM.csv', 'wb') as fx:
    fx.write(csv)

I'm getting the following error message.

IOError: ('http error', 401, 'Unauthorized', <httplib.HTTPMessage instance at 0x037541E8>)
Lee Murray
  • 315
  • 1
  • 6
  • 19
  • The error you get says it all. You are not authorized to access the resource. It depends on the server if it accepts any authorization header, which you will have to send along with the request. – Terry Apr 27 '16 at 11:08
  • Hmmm, but if I put the URL in manually I get access to download the file. – Lee Murray Apr 27 '16 at 11:20

1 Answers1

0

Try something like this:

import urllib2,base64
import csv
username ="username"
password= "password"
url = 'https://office.com/sites/20Reporting/Lists/Reports/476%20-%2050%20DaySDShrink%20Report/476-%2030%20DaySDShrink.csv'
request = urllib2.Request(url )
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)   
csv = urllib2.urlopen(request).read()
with open('C:\Users\Documents\DPAM.csv', 'wb') as fx:
    fx.write(csv)

Also you can try to communicate with sharepoint via SOAP using urllib2

Community
  • 1
  • 1
Valeriy Solovyov
  • 5,384
  • 3
  • 27
  • 45