使用Python将数据提交到表单的最佳方法?

时间:2010-10-13 20:09:27

标签: python forms

之前我没有使用网络编程或网络表单,所以我迷失在这里。有一个简单的perl / cgi

<form method="post" action="/gestalt/cgi-pub/Kaviar.pl" enctype="multipart/form-data">

现在我尝试在这里查看问题,进行了谷歌搜索并阅读了一些关于urllib2等的信息。我想我对此知之甚少,从所有那些离开或整合的地方拿起并以有意义的方式使用他们的例子解决我的问题。 这是页面 http://db.systemsbiology.net/gestalt/cgi-pub/Kaviar.pl 我想通过python使用这个页面,提交数据并检索它并在我的脚本中解析它。 样本数据是这样的

chr1:4793
chr1:53534
chr1:53560

所以问题是,你能帮助我如何提交数据并将结果一步一步地反馈到python脚本中,还是请你指导我一个简单的,循序渐进的指南,教导如何做到这一点? 感谢

1 个答案:

答案 0 :(得分:4)

这应该是一个好的开始:

import urllib, urllib2
url = 'http://db.systemsbiology.net/gestalt/cgi-pub/Kaviar.pl'
form_data = {'chr':'chr1', 'pos':'46743'} # the form takes 2 parameters: 'chr', and 'pos'
                                          # the values given in the dict are
                                          # just examples.
# the next line POSTs the form to url, and reads the resulting response (HTML
# in this case) into the variable response
response = urllib2.urlopen(url,urllib.urlencode(form_data)).read()
# now you can happily parse response.