Reputation: 23
I am trying to download an excel file from a website. I successfully filled the form using mechanize, and submitting the form should be returning me a file download. but when it comes to downloading, it is returning me the html rather than the actual contents of the file.
import mechanize
br = mechanize.Browser()
br.open("http://web.sba.gov/pro-net/search/dsp_dsbs.cfm")
br.select_form('SearchForm')
br["States"] = ["AL","AK"]
br["E8a"] = ["Y"]
br["Report"] = ["S"]
response = br.submit()
fileobj = open("szz.txt","wb")
fileobj.write(response.read())
fileobj.close()
the result looks like
<!doctype html>
<html lang="en-US" dir="ltr">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<title>SBA - Dynamic Search</title>
<link href="/gls/dsp_choosefunction.cfm" accesskey="1" rel="Home" title="Home (Return to GLS Choose Function)">
<link rel="stylesheet" type="text/css" media="all" href="/library/css/jquery.mobile/sba.dtv.css?CachedAsOf=2012-06-20T22:15"/><!-- local code -->
<link rel="stylesheet" type="text/css" media="all" href="/library/css/sczz.strict.css?CachedAsOf=2013-09-20T18:55"/>
<script src="/library/javascripts/jquery/jquery.js?CachedAsOf=2012-09-21T15:37"></script><!-- 1.8.2 -->
<script src="/library/javascripts/jquery/jquery.mobile/sba.jqm.js?CachedAsOf=2013-03-28T16:11"></script><!-- local code -->
<noscript>
<link rel="stylesheet" type="text/css" media="all" href="/library/css/sczz.noscript.css?CachedAsOf=2010-10-14T19:23"/>
</noscript>
<script>
var gSlafDevTestProd = "Prod";
var gSlafDevTestProdInd = "2";
var gSlafInlineBlock = "inline-block";
Upvotes: 2
Views: 3556
Reputation: 9102
I found a couple of mistakes on your code, I tried the following code and opening the file in a browsers displays a nice table, so try it:
import mechanize
br = mechanize.Browser()
br.open("http://web.sba.gov/pro-net/search/dsp_dsbs.cfm")
br.select_form('SearchForm')
br.form["State"] = ["AL","AK"]
br.form["E8a"] = ["Y"]
br.form["Report"] = ["S"]
response = br.submit()
fileobj = open("szz.html","wb")
fileobj.write(response.read())
fileobj.close()
basically you need to call br.form[control_name]
and you had a mistake on the key "States" it is just "State", now save the file as .html
and open it in a browser to see if that is what you were looking for.
Upvotes: 3