你指的是網頁上的表格有一個單獨的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)
這將提交一份工作與你的輸入數據,它並不直接返回結果。要檢查結果,有必要從以前的響應中提取作業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)
非常感謝你,它解決了我的問題,而是如何找到正確的網址?當我輸入正確的到我的瀏覽器,它來到我已經包括了一個例子,舊錯一個又...... –
@NingWang - 然而,也有在本地安裝libclustalo和使用clustalo包(一個選項Python包裝) – ewcz
,謝謝你的詳細解答。如果下一次,我做了另一個任務,我怎麼知道「http://www.ebi.ac.uk/Tools/services/web_clustalo/toolform.ebi」是正確的,而不是我從瀏覽器複製的網址? –