2016-10-07 12 views
0

これは私がPythonで投稿したいフォームです:Pythonを使用してこのHTMLフォームを投稿するにはどうすればよいですか?

<FORM METHOD="POST" 
    ACTION="http://www.speech.cs.cmu.edu/cgi-bin/tools/lmtool/run" 
    ENCTYPE="multipart/form-data"> 
    <INPUT NAME="formtype" TYPE="HIDDEN" value="simple"> 

    <p><b>Upload a sentence corpus file</b>:<br> 
     <INPUT NAME="corpus" TYPE="FILE" SIZE=60 VALUE="empty"> 
    </p> 

     <INPUT TYPE="submit" VALUE="COMPILE KNOWLEDGE BASE"> 
    </form> 

私はリクエスト私はこのフォームを自動化する必要が

import requests 

url = "http://www.speech.cs.cmu.edu/cgi-bin/tools/lmtool/run" 
#url = "http://httpbin.org/post" 
files = {'file': open('testfile', 'rb')} 
payload = {'NAME': 'fromtype', 'TYPE': 'HIDDEN', 'value': 'simple', 
'NAME': 'corpus', 'TYPE': 'FILE', 'SIZE': '60', 'VALUE': 'empty'} 
r = requests.post(url, data=payload) 

print r.text 

と応答

Running: /home/darthtoker/programs/post.py (Fri Oct 7 15:19:21 2016) 

<!DOCTYPE html 
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"> 
<head> 
<title>LMTool Error</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
</head> 
<body> 
<pre>Something went wrong. This is all I know: formtype 
</pre> 
</body> 
</html>Content-Type: text/html; charset=ISO-8859-1 

<!DOCTYPE html 
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"> 
<head> 
<title>LMTool Error</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
</head> 
<body> 
<pre>Something went wrong. This is all I know: corpus 
</pre> 
</body> 
</html>Status: 302 Found 
Location: http://www.speech.cs.cmu.edu/tools/product/1475867962_31194 

でそれを試してみました私が取り組んでいるプロジェクト、助けてください!

答えて

0

あり、他のエラーであってもよいが、あなたが得るエラーメッセージが十分に明確であるかもしれません:

フォームが期待するもの:

<INPUT NAME="formtype" ...> 

あなたが送る何:

'NAME': 'fromtype', ... 

あなたが見ますフォームタイプfromタイプの違いは?


RequestsMore complicated POST requestsPOST a Multipart-Encoded File)のドキュメンテーションによると、あなたのコードは次のようになります。

import requests 

url = "http://www.speech.cs.cmu.edu/cgi-bin/tools/lmtool/run" 
#url = "http://httpbin.org/post" 
files = {'corpus': open('testfile', 'rb')} 
payload = {'formtype': 'simple' } 
r = requests.post(url, data=payload, files=files) 

print r.text 
+0

あなたは、時にはシンプルなものであることを知っています。ありがとう、私はそれを完全に逃した。失読症の瞬間をしている必要があります。 –

+0

ええ、まだ私はそれを取得しないでください –

+0

おかげで私はどこが間違っていた参照してください。わかった。本当にありがとう。 –

関連する問題