gpt4 book ai didi

Python Mechanize 文件下载

转载 作者:太空宇宙 更新时间:2023-11-04 03:52:45 26 4
gpt4 key购买 nike

我正在尝试从网站下载 excel 文件。我使用 mechanize 成功填写了表格,提交表格应该会返回给我一个文件下载。但是在下载时,它返回的是 html 而不是文件的实际内容。

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

结果是这样的

<!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";

最佳答案

我在您的代码中发现了几个错误,我尝试了以下代码并在浏览器中打开文件显示了一个漂亮的表格,所以试试看:

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

基本上你需要调用 br.form[control_name] 并且你在键“States”上有一个错误它只是“State”,现在将文件保存为 .html 并在浏览器中打开它,看看这是否是您要查找的内容。

关于Python Mechanize 文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20412157/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com