¿Cómo uso python mechanize para recuperar un archivo de aspnetForm submitControl que desencadena la descarga de un archivo Excel cuando no conozco la URL del archivo o el nombre del archivo?python mecanize - recuperar un archivo de aspnetForm submitControl que activa la descarga de un archivo
URL del sitio con el archivo de Excel: http://www.ncysaclassic.com/TTSchedules.aspx?tid=NCFL&year=2012&stid=NCFL&syear=2012&div=U11M01
estoy tratando de obtener el archivo descargado por 'botón' Imprimir Excel.
hasta ahora tengo:
r = br.open('http://www.ncysaclassic.com/TTSchedules.aspx?tid=NCFL&year=2012&stid=NCFL&syear=2012&div=U11M01')
html = r.read()
# Show the html title
print br.title()
# Show the available forms
for f in br.forms():
print f
br.select_form('aspnetForm')
print '\n\nSubmitting...\n'
br.submit("ctl00$ContentPlaceHolder1$btnExtractSched")
print 'Response...\n'
print br.response().info()
print br.response().read
print 'still alive...\n'
for prop, value in vars(br.response()).iteritems():
print 'Property:', prop, ', Value: ', value
print 'myfile...\n'
myfile = br.response().read
y me da este resultado:
Submitting...
Response...
Content-Type: application/vnd.ms-excel
Last-Modified: Thu, 27 Sep 2012 20:19:10 GMT
Accept-Ranges: bytes
ETag: W/"6e27615aed9ccd1:0"
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Thu, 27 Sep 2012 20:19:09 GMT
Connection: close
Content-Length: 691200
<bound method response_seek_wrapper.read of <response_seek_wrapper at 0x2db5248L whose wrapped object = <closeable_response at 0x2e811c8L whose fp = <socket._fileobject object at 0x0000000002D79930>>>>
still alive...
Property: _headers , Value: Content-Type: application/vnd.ms-excel
Last-Modified: Thu, 27 Sep 2012 20:19:10 GMT
Accept-Ranges: bytes
ETag: W/"6e27615aed9ccd1:0"
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Thu, 27 Sep 2012 20:19:09 GMT
Connection: close
Content-Length: 691200
Property: _seek_wrapper__read_complete_state , Value: [False]
Property: _seek_wrapper__have_readline , Value: True
Property: _seek_wrapper__is_closed_state , Value: [False]
Property: _seek_wrapper__pos , Value: 0
Property: wrapped , Value: <closeable_response at 0x2e811c8L whose fp = <socket._fileobject object at 0x0000000002D79930>>
Property: _seek_wrapper__cache , Value: <cStringIO.StringO object at 0x0000000002E8B0D8>
Parece que estoy muy cerca ... Tenga en cuenta el Content-Type: application/vnd.ms-excel
Simplemente no sé qué hacer a continuación. ¿Dónde está mi archivo y cómo puedo obtener un puntero y guardarlo localmente para acceder más tarde?
Actualización:
Solía dir() para obtener una lista de los métodos/atributos para la respuesta() y luego probado un par de los métodos ...
print '\ndir(br.response())\n'
for each in dir(br.response()):
print each
print '\nresponse info...\n'
print br.response().info()
print '\nresponse geturl\n'
print br.response().geturl()
y me sale esto salida ...
dir(br.response())
__copy__
__doc__
__getattr__
__init__
__iter__
__module__
__repr__
__setattr__
_headers
_seek_wrapper__cache
_seek_wrapper__have_readline
_seek_wrapper__is_closed_state
_seek_wrapper__pos
_seek_wrapper__read_complete_state
close
get_data
geturl
info
invariant
next
read
readline
readlines
seek
set_data
tell
wrapped
xreadlines
response info...
Date: Thu, 27 Sep 2012 20:55:02 GMT
ETag: W/"fa759b5df29ccd1:0"
Server: Microsoft-IIS/7.5
Connection: Close
Content-Type: application/vnd.ms-excel
X-Powered-By: ASP.NET
Accept-Ranges: bytes
Last-Modified: Thu, 27 Sep 2012 20:55:03 GMT
Content-Length: 691200
response geturl
http://www.ncysaclassic.com/photos/pdftemp/ScheduleExcel165502.xls
Creo que ya tengo este archivo en mi br.response. ¡Simplemente no sé cómo extraerlo! Por favor ayuda.
Estoy más cerca que parece ... – hokie85
Estos ambos trabajaron para mí: de impresión '\ nAttempting escribir el archivo 1 ... \ n' # encontraron esta aquí http: // stackoverflow.com/questions/8116623/how-to-download-a-file-in-python # open ("/ path/to/someFile", "wb") .write (urllib2.urlopen ("http: // someUrl.com/somePage.html "). read()) open (" C: \ Users \ gregb \ Downloads \ download.xls "," wb "). write (br.response(). read()) print '\ nSe está intentando escribir el archivo 2 ... \ n' open ("C: \ U sers \ gregb \ Downloads \ urllib2_urlopen.xls "," wb ") .write (urllib2.urlopen (" http://www.ncysaclassic.com/photos/pdftemp/ScheduleExcel172625.xls ") .read()) – hokie85