如何解决程序化映射UniProt ID时出现的404/405客户端错误
UniProt ID映射脚本错误解决方案
第一个脚本(idmapping/run 500内部服务器错误)
原代码使用GET请求调用/idmapping/run接口,但该接口仅支持POST请求,同时需确保协议为HTTPS(错误返回中的HTTP URL可能是API内部跳转异常导致)。修正后的代码如下:
import requests API_URL = "https://rest.uniprot.org" def submit_id_mapping(from_db, to_db, ids): request = requests.post( f"{API_URL}/idmapping/run", data={"from": from_db, "to": to_db, "ids": ",".join(ids)}, headers={"Content-Type": "application/x-www-form-urlencoded"} ) request.raise_for_status() # 取消注释可快速定位请求错误 return request.json() submit_id_mapping(from_db="UniProtKB_AC-ID", to_db="ChEMBL", ids=["P05067", "P12345"])
错误原因:
- 官方文档示例可能存在更新滞后,
/idmapping/run接口仅接受POST请求,GET请求会触发服务器内部错误。 - 请求需使用HTTPS协议,避免HTTP与HTTPS协议不一致引发的跳转问题。
第二个脚本(uploadlists 405方法不允许错误)
原代码使用POST请求调用uploadlists接口,但该接口仅支持GET请求,导致405错误。修正后的代码如下:
import urllib.parse import urllib.request in_f = open("filename") url = 'https://www.uniprot.org/uploadlists/' ids = in_f.read().splitlines() ids_s = " ".join(ids) params = { 'from': 'PDB_ID', 'to': 'ACC', 'format': 'tab', 'query': ids_s } # 将参数编码后拼接至URL,使用GET请求 url_with_params = url + '?' + urllib.parse.urlencode(params) req = urllib.request.Request(url_with_params, method='GET') with urllib.request.urlopen(req) as f: response = f.read() print(response.decode('utf-8'))
错误原因:
uploadlists接口设计为仅接受GET请求,参数需通过URL查询字符串传递,而非POST请求体。
内容的提问来源于stack exchange,提问作者Caterina
相关产品推荐
相关产品推荐

