2012-09-27 24 views
5

Làm cách nào để sử dụng cơ chế python để truy xuất tệp từ một tệp tin aspnetForm submitControl kích hoạt tệp tải xuống Excel khi tôi không biết URL tệp hoặc tên tệp?python cơ giới hóa - truy xuất một tệp từ aspnetForm submitControl kích hoạt tệp tải xuống

URL của trang web với file Excel: http://www.ncysaclassic.com/TTSchedules.aspx?tid=NCFL&year=2012&stid=NCFL&syear=2012&div=U11M01

Tôi đang cố gắng để có được các tập tin tải về bằng cách 'nút' Print Excel.

Cho đến nay tôi có:

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 

và tôi nhận được kết quả này:

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> 

Có vẻ tôi rất gần ... Lưu ý Content-Type: application/vnd.ms-excel

Tôi chỉ không biết phải làm gì tiếp theo. Tệp của tôi ở đâu và làm thế nào để tôi có được một con trỏ đến nó và lưu nó cục bộ để truy cập sau này?

Cập nhật:

tôi đã sử dụng dir() để có được một danh sách các phương pháp/thuộc tính cho phản ứng() và sau đó thử một vài phương pháp ...

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() 

và tôi có được điều này đầu ra ...

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 

Tôi nghĩ rằng tôi đã có tệp này trong tài liệu của tôi. Tôi chỉ không biết cách giải nén nó! Hãy giúp tôi.

+0

tôi nhận được gần gũi hơn có vẻ như ... – hokie85

+0

Những cả làm việc cho tôi: in '\ nAttempting ghi tệp 1 ... \ n' # tìm thấy điều này ở đây http: // stackoverflow.com/questions/8116623/how-to-download-a-file-in-python # mở ("/ path/to/someFile", "wb") .write (urllib2.urlopen ("http: // đọc()) mở ("C: \ Users \ gregb \ Downloads \ download.xls", "wb") viết (br.response(). đọc()) in '\ nĐảm bảo ghi tệp 2 ... \ n' mở ("C: \ U sers \ gregb \ Downloads \ urllib2_urlopen.xls "," wb ") .write (urllib2.urlopen (" http://www.ncysaclassic.com/photos/pdftemp/ScheduleExcel172625.xls ") .read()) – hokie85

Trả lời

3
# fill out the form 
response = br.submit() 
fileobj = open('filename', 'w+') 
fileobj.write(response.read()) 
fileobj.close() 
+0

Cảm ơn Điều đó đã làm việc – hokie85

+0

Tại sao tôi không thể nhập một dấu xuống dòng trong các ý kiến ​​này? Khi tôi làm, nhận xét của tôi được gửi trước khi tôi hoàn tất! – hokie85

+0

Hãy thử hai khoảng trống được đề xuất trong phần trợ giúp – hokie85

Các vấn đề liên quan