我正在尝试使用 http://www.camp.bicnirrh.res.in/featcalc/通过 python 2.7 POST multipart/form-data
。具体来说,我正在上传一个 FASTA
格式的文件(名为 'Practice.txt'
),基本上是这样的格式:
'>1(ENTER)STRINGOFSPECIFICCAPITALLETTERS'
到这个网站,它还有一个文本框,您也可以在其中手动输入数据(我将其留空)。这个数据网站还有复选框选项,我想选择其中'Length'
、'Net Charge'
、'Aliphatic Index'
和“疏水性”
。页面底部有一个“提交”
按钮。
目前,这是我用于 POST
响应的代码。
files = {'file': ('Practice.txt', open('Practice.txt', 'rb'))}
data = {'Length':'Length', 'Net Charge':'Net Charge', 'Aliphatic Index':'Aliphatic Index','Hydrophobicity':'Hydrophobicity'}
r = requests.post(url, files=files, data=data)
r.text
问题是,当我执行r.text
时,这些都不会返回任何数据。使用浏览器时,网站会计算所有这些内容的值。我有 WireShark,并且我一直在尝试查看实时源以查看我到底发送到服务器的内容,尽管我逐字使用上面的代码,但它并没有给出返回浏览器所返回的值。
有人知道为什么会发生这种情况/如何实际获取数据吗?感谢您的任何意见!
最佳答案
这会起作用:
import requests
import urllib
session = requests.Session()
file={'file':(open('practice.txt','r').read())}
url = 'http://www.camp.bicnirrh.res.in/featcalc/tt1.php'
payload = {
'length' :'length', #Length
'netcharge':'netcharge', #Net Charge
'aliphatic':'aliphatic', #Aliphatic Index
'gravy' :'gravy' #Hydrophobicity
}
raw = urllib.urlencode(payload)
response = session.post(url, files=file, data=payload)
print(response.text)
所有选项:
payload = {
'length' :'length', #Length
'netcharge' :'netcharge', #Net Charge
'amino' :'amino', #Amino acid composition
'aliphatic' :'aliphatic', #Aliphatic Index
'instability':'instability', #Instability Index
'gravy' :'gravy', #Hydrophobicity
'sec' :'sec' #Secondary Structure Propensity
}
关于python - 使用 request.post 通过 python 发布多部分表单数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24255795/