填写HTML表单并提交GET请求Python 3

时间:2017-12-08 14:10:37

标签: python html forms urllib

嗨Python爱好者,

我正在努力使用Python 3中的 urllib.request 传递参数来填写并提交HTML表单。

html代码的关键部分如下所示:

<H1>Header</H1>
<form method="get" action="/bin/formula.php">
<ul>
 <li>Overview <A HREF="/kathegory.php">Kathegory</A> 
 <li><A HREF="/bin/search.php">Search</A>
 <li><A HREF="/bin/formula.php">Formula</A>
<li>go to:
  <input type="text" name="id" size=10 maxlength=10>
  <input type="submit" value="go">**
</ul>
</form>
<ul>
 <li><A HREF="history.php">History</A>
</ul>
<P>
<HR>

在网站上手动填写表单(例如 1 http://example.com 时,结果页面将被称为 {{3} }

到目前为止,我在Python中的方法是:

from bs4 import BeautifulSoup
import urllib.request
import urllib.parse


h = {'User-agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
payload = {'id': '1'}
url = 'http://example.com'

data = urllib.parse.urlencode(payload)
#data = data.encode('ascii') # data should be bytes
req = urllib.request.Request(url, data, h)
print(req.__dict__)

with urllib.request.urlopen(req) as response:
the_page = response.read()

print(the_page)

没有成功。

非常感谢来自更有经验的用户的任何建议!

干杯

1 个答案:

答案 0 :(得分:0)

使用非常受欢迎的请求(“人类的HTTP”)包:http://docs.python-requests.org

相关问题