#python #html #post #python-requests
#python #HTML #Публикация #python-запросы
Вопрос:
Это форма, которую я хочу опубликовать на Python:
<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
Мне нужно автоматизировать эту форму для проекта, над которым я работаю, пожалуйста, помогите!
Ответ №1:
Могут быть и другие ошибки, но сообщение об ошибке, которое вы получаете, достаточно ясно:
Что ожидает форма:
<INPUT NAME="formtype" ...>
что вы отправляете:
'NAME': 'fromtype', ...
Вы видите разницу между formtype и fromtype?
В соответствии с документацией запросов (более сложные POST-запросы, POST-файл с кодировкой из нескольких частей) ваш код должен быть:
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
Комментарии:
1. Вы знаете, иногда это простые вещи. Спасибо, я совершенно пропустил это мимо ушей. Должно быть, у вас дислексический момент.
2. да, я все еще не понимаю
3. Хорошо, круто, я вижу, где я ошибся. теперь я понял. большое спасибо.