为什么 requests.post 使用 Clustal Omega 服务没有响应?

Why requests.post have no response with Clustal Omega service?

import requests
MSA_request=""">G1
MGCTLSAEDKAAVERSKMIDRNLREDGEKAAREVKLLLL
>G2
MGCTVSAEDKAAAERSKMIDKNLREDGEKAAREVKLLLL
>G3
MGCTLSAEERAALERSKAIEKNLKEDGISAAKDVKLLLL"""
q={"stype":"protein","sequence":MSA_request,"outfmt":"clustal"}
r=requests.post("http://www.ebi.ac.uk/Tools/msa/clustalo/",data=q)

这是我的脚本,我向网站发送了这个请求,但结果看起来我什么也没做,网络服务没有收到我的请求。这个方法以前用其他网站没问题,也许这个页面弹出window询问cookie协议?

您所指页面上的表格有一个单独的 URL,即

http://www.ebi.ac.uk/Tools/services/web_clustalo/toolform.ebi

您可以在浏览器中使用 DOM 检查器来验证这一点。 因此,为了继续requests,您需要访问正确的页面

r=requests.post("http://www.ebi.ac.uk/Tools/services/web_clustalo/toolform.ebi",data=q)

这将使用您输入的数据提交作业,它不会直接 ​​return 结果。要检查结果,需要从之前的响应中提取作业 ID,然后生成另一个请求(没有数据)到

http://www.ebi.ac.uk/Tools/services/web_clustalo/toolresult.ebi?jobId=...

但是,您一定要检查此编程访问是否与该网站的 TOS 兼容...

这是一个例子:

from lxml import html
import requests
import sys
import time

MSA_request=""">G1
MGCTLSAEDKAAVERSKMIDRNLREDGEKAAREVKLLLL
>G2
MGCTVSAEDKAAAERSKMIDKNLREDGEKAAREVKLLLL
>G3
MGCTLSAEERAALERSKAIEKNLKEDGISAAKDVKLLLL"""
q={"stype":"protein","sequence":MSA_request,"outfmt":"clustal"}

r = requests.post("http://www.ebi.ac.uk/Tools/services/web_clustalo/toolform.ebi",data = q)
tree = html.fromstring(r.text)
title = tree.xpath('//title/text()')[0]

#check the status and get the job id
status, job_id = map(lambda s: s.strip(), title.split(':', 1))
if status != "Job running":
    sys.exit(1)

#it might take some time for the job to finish
time.sleep(10)

#download the results
r = requests.get("http://www.ebi.ac.uk/Tools/services/web_clustalo/toolresult.ebi?jobId=%s" % (job_id))

#prints the full response
#print(r.text)

#isolate the alignment block
tree = html.fromstring(r.text)
alignment = tree.xpath('//pre[@id="alignmentContent"]/text()')[0]
print(alignment)