python mechanize-从aspnetForm submitControl检索触发文件下载的文件

python mechanize-从aspnetForm submitControl检索触发文件下载的文件,python,file,download,mechanize,Python,File,Download,Mechanize,当我不知道文件URL或文件名时,如何使用python mechanize从aspnetForm submitControl检索文件,该控件会触发Excel文件下载 包含Excel文件的站点的URL: 我正在尝试通过打印Excel“按钮”下载文件 到目前为止,我已经: r = br.open('http://www.ncysaclassic.com/TTSchedules.aspx?tid=NCFL&year=2012&stid=NCFL&syear=2012&di

当我不知道文件URL或文件名时,如何使用python mechanize从aspnetForm submitControl检索文件,该控件会触发Excel文件下载

包含Excel文件的站点的URL:

我正在尝试通过打印Excel“按钮”下载文件

到目前为止,我已经:

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
我得到这个输出:

    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>
我得到这个输出

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

我想我的br.response中已经有了这个文件。我只是不知道如何提取它!请帮助。

我似乎离得越来越近了……这两种方法对我都很有效:print'\n尝试编写文件1…\n'#在这里找到它#open(“/path/to/someFile”,“wb”).write(urllib2.urlopen”()open(“C:\Users\gregb\Downloads\download.xls”,“wb”).write(br.response().read())打印“\n尝试写入文件2…\n”打开(“C:\Users\gregb\Downloads\urlib2\u urlopen.xls”,“wb”)。写入(urlib2.urlopen(“)为什么我不能在这些评论中输入回车符?当我输入回车符时,我的评论在我完成之前就提交了!让我们试试帮助中建议的两个空格。让我们再试一次。这会开始一行新的内容吗?在IE上试一下,而不是Chrome。我本来打算在评论中发布我的代码,但是如果我不能输入换行符,那就很难了……让我试试标记格式化…
#这很有效!#打开一个本地文件实例fileobj=open(“C:\\Users\\gregb\\Downloads\\ncysa\u schedule.xls”,“w+”)#从fileobj.write(br.response().read())fileobj.close()上面的提交响应写入它。
你知道如何隐藏gzip(True)警告吗?
br.set\u handle\u gzip(True)#这会给出警告-如何抑制它?
br.set_handle_gzip(True)#这会给出警告-如何抑制它`
# fill out the form
response = br.submit()
fileobj = open('filename', 'w+')
fileobj.write(response.read())
fileobj.close()
# fill out the form
response = br.submit()
fileobj = open('filename', 'w+')
fileobj.write(response.read())
fileobj.close()