我正在尝试使用Python的请求包自动处理网页上的某些数据,但我只是获得“响应[403]”作为返回值。我究竟做错了什么?这是最小的(不起作用的)示例:
import requests
url = 'http://tools.iedb.org/mhcii/'
seq = "GGPKLRGNVTSNIKFPSDNKGKIIRGSNDKLNKNSEDVLEQSEKSLVSENVPSGLDIDDI"
form_data = {'sequence_text': seq,
'submit': 'submit'}
response = requests.post(url, data=form_data)
print(response)
我还尝试按照here和here中的建议,为它提供一个带有浏览器信息的附加标头,但响应仍然保持不变。想法?
更新:
我已合并以下建议,403错误消失了!现在,网络表单将返回一个有效的htm页面,其中包含消息“您必须选择一个等位基因”。我不太确定自己在做什么错,因为我在请求中设置了“ allele_list”:
import requests
url = 'http://tools.iedb.org/mhcii/'
seq = "GGPKLRGNVTSNIKFPSDNKGKIIRGSNDKLNKNSEDVLEQSEKSLVSENVPSGLDIDDI"
client = requests.session()
# retrieve the CSRF token
client.get(url) # sets cookie
if 'csrftoken' in client.cookies:
csrftoken = client.cookies['csrftoken'] # Django 1.6 and up
else:
csrftoken = client.cookies['csrf'] # older versions
form_data = {"sequence_text": seq,
"method": "1",
"sequence_format": "fasta",
"locus_list": "Human, HLA-DR",
"allele_list": "DRB1*01:01", # or HLA-DRB1*01:01
"sort_output": "MHC_IC50",
"output_format": "ascii",
"submit": "submit",
"csrfmiddlewaretoken": csrftoken}
header_data = {"Referer": url,
"User-Agent": "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"}
r = client.post(url, data=form_data, headers=header_data)
print(r.text)