简体   繁体   English

Python:如何设置requests.post

[英]Python: how to set up a requests.post

I am trying to programmatically use this tool http://www.idtdna.com/calc/analyzer so that I can batch analyze a few dozen DNA sequences. 我正在尝试以编程方式使用此工具http://www.idtdna.com/calc/analyzer,以便可以批量分析几十个DNA序列。

Peeking inside Chrome's developer tools, I see that I am sending a POST request when I click the Analyze button (after entering a sequence (eg "AACCGGTT") in the Sequence field). 在Chrome的开发人员工具中查看时,我看到我单击Analyze按钮时(在“序列”字段中输入序列(例如“ AACCGGTT”)后)正在发送POST请求。 In the Response tab, I can also see the response (eg "MeltTemp") that I wish to collect. 在“响应”选项卡中,我还可以看到希望收集的响应(例如“ MeltTemp”)。 So, following the tutorial ( http://docs.python-requests.org/en/latest/user/quickstart/#make-a-request ), I put together the snippet below, but it's clearly not working. 因此,按照本教程( http://docs.python-requests.org/en/latest/user/quickstart/#make-a-request ),我整理了以下代码段,但显然无法正常工作。

>>> import requests
>>> url = 'http://www.idtdna.com/calc/analyzer/home/analyze'  # from: ChromeDevTools -> Network -> Name: analyze -> tab: Headers -> General -> Request URL
>>> data = {"settings":{"Sequence":"AACCGGTT",  # from: ChromeDevTools -> Network -> Name: analyze -> tab: Headers -> Request Payload
                        "NaConc":50,
                        "MgConc":0,
                        "DNTPsConc":0,
                        "OligoConc":0.25,
                        "NucleotideType":"DNA",
                       }}

>>> r = requests.post(url, data=data)

>>> r.url  # 404 page
u'http://www.idtdna.com/404.aspx?aspxerrorpath=/calc/analyzer/home/analyze'

# I was hoping for something like this that gives me the JSON response (which can be found at
# ChromeDevTools -> Network -> Name: analyze -> tab: Response)
>>> r.some_magical_function()
{"Sequence":"AAC CGG TTG GTT AAT T","NaConc":50,  ... "MeltTemp":45.1, ...}

What am I missing? 我想念什么?

Does the post request need to be way more complicated (with cookies? session??)? 发布请求是否需要更复杂(使用Cookie?会话?)? If so, please provide pointers to what I need to learn (or the solution :p) 如果是这样,请提供指向我需要学习的知识(或解决方案:p)

I understand if the website has safeguards against this kind of usage; 我了解该网站是否可以防止此类使用; if it's basically impossible, then what strategy do you suggest?selenium? 如果基本上不可能,那么您建议采取什么策略?硒?


import requests

url = 'http://sg.idtdna.com/calc/analyzer/home/analyze'
data = {
    'settings': {
        'Sequence': 'AACCGGTT',
        'NaConc': 50,
        'MgConc': 0,
        'DNTPsConc': 0,
        'OligoConc': 0.25,
        'NucleotideType': 'DNA',
    }
}

s = requests.Session()
s.get('http://sg.idtdna.com/calc/analyzer')  # to set cookies
r = s.post(url, json=data)
print(r.json())

UPDATE 更新

Depending on location, redirection does not happen. 根据位置,不会发生重定向。 In such case use www.idtdna.com instead of sg.idtdna.com . 在这种情况下,请使用www.idtdna.com而不是sg.idtdna.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM