使用Python提交数据到表单的最佳方法是什么?
我之前没有接触过网页编程或网页表单,所以现在有点迷茫。这里有一个简单的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 个回答
5
这应该是一个不错的开始:
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.