How to make POST request in robobrowser-python

二次信任 提交于 2019-12-07 04:23:17

问题


http://robobrowser.readthedocs.org/en/latest/api.html

I'm trying to hit an API using browser.open(myurl) and looking for a RB method similar to requests.post(url,data=data).

since robobrowser built on the top of requests and beautifulsoup so I think there must be a way to do so.


回答1:


You can re-use the requests.Session() object of a RoboBrowser object to do your own POSTs with the same cookies:

response = browser.session.post(...)

The RoboBrowser state won't be updated though; you'd have to pass the response object to the RoboBrowser._update_state() method.

browser._update_state(response)

For AJAX requests you wouldn't need to do this, however.

Take into account that future versions of the library may change how that works; it's not a documented method.

However, normally you'd post to a site because you are handling a form submit; leave that to the provided library functionality (RoboBrowser.get_form(), then RoboBrowser.submit_form()).




回答2:


As of version 0.5.3, you can send POST requests like this:

browser.open(url, method='post', data=data)

And of-coarse the browser's state and session will be updated automatically.

  • Related resolved issue on github.


来源:https://stackoverflow.com/questions/23737719/how-to-make-post-request-in-robobrowser-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!