一日中、私はこれを解決しようとしています。このhttp://www.some.site/index.phpがあり、ユーザーとパスワードを要求し、Cookie を送信します。わかりました、私はこのように入ります:
import urllib, urllib2, cookielib, os
import re # not required here but tried it out though
import requests # not required here but tried it out though
username = 'somebody'
password = 'somepass'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'j_password' : password})
resp = opener.open('http://www.some.site/index.php', login_data)
print resp.read()
問題は、画面の中央に .xls ファイルをダウンロードするためのリンクがあることです: http://www.some.site/excel_file.php?/t=1303457489。任意のブラウザ (Mozilla、Chrome、IE) でファイルをダウンロードできますが、Python ではダウンロードできません。.php の後、投稿データ(つまり: ?t=1370919996 ) は、ログインまたはページの更新時に常に変更されます。
たぶん私は間違っていますが、投稿データは Cookie (またはセッション Cookie) から生成されていると思いますが、Cookie にはこれしか含まれていません。('set-cookie', 'PHPSESSID=9cde55534fcc8e136fcf6588c0d0f1df; path=/')
これは、ファイルを保存しようとした 1 つの方法です。
print "downloading with urllib2"
f = urllib2.urlopen('http://www.some.site/excel_file.php')
data = f.read()
with open("exceldoc.xls", "wb") as code:
code.write(data)
保存するか印刷すると、同じ悪い要求エラーが発生します。
<b>Fatal error</b>: Call to a member function FetchRow() on a non-object in <b>http://www.some.site/excel_file.php</b> on line <b>112</b><br
このファイルを Python でダウンロードするにはどうすればよいですか? 助けてくれてありがとう!
多くの同様の投稿があります。私はそれらをチェックしましたが、私の例はそれらからインスピレーションを得ていますが、何もうまくいきませんでした。私はクッキー、php、js にあまり詳しくありません。
編集: これは、index.php の内容を印刷したときに得られるものです。
<html>
<head>
<title>SOMETITLE</title>
<meta http-equiv="Page-Enter" content="blendTrans(Duration=0.5)">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel='stylesheet' type='text/css' href='somesite.css'>
<SCRIPT LANGUAGE="JavaScript">
<!-- JavaScript hiding
function clearDefault(obj) {
if (!obj._cleared) {
obj.value='';
obj._cleared=true;
}
}
// -->
</SCRIPT>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="1000" height="150" border="0" align="center" cellpadding="16" cellspacing="0" class="header" style="background: #989896 url('images/header.png') no-repeat;">
<tr>
<td valign="middle">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="380"> </td>
<td>
<div id="login">
<form name="flogin" method="post" action="/index.php">
<h1>Login</h1>
<input name="uName" type="text" value="Username:" class="name" onfocus="clearDefault(this)">
<br>
<input type="password" name="uPw" value="Password:" class="pass" onfocus="clearDefault(this)">
<input type="submit" name="Submit" value="OK" class="submit">
</form>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>