Combining RoboBrowser with Requests-HTML

风格不统一 提交于 2019-12-10 12:18:34

问题


I like the ease of filling out and submitting online forms using RoboBrowser and I think I understand how to access the requests.Session() instance underlying RoboBrowser if I need to use that.

But I want to submit a form using RoboBrowser then pass the session to requests_html.Session() so I can render the HTML using JavaScript. How do I do that? Is there a way to convert a Requests session to a Requests-HTML session?

I have looked through the documentation for Requests, Requests-HTML and RoboBrowser, as well as through all SO questions about Requests-HTML. I have also Googled for the answer. None of these sources helped.

I am aware that it might be easier to use Selenium for this purpose, but this is for a project at work, where I cannot install Selenium. I believe my broader question of how to convert or pass a Requests session to a Requests-HTML session is a useful one for the Python community.


回答1:


I found the answer in the Requests-HTML source code. There is a specific class method called HTMLResponse._from_response() for this purpose, which takes a response as its first argument and a session as its second argument.

Say we have a robobrowser.RoboBrowser() object named browser. Then the underlying requests.Response() object is accessible by browser.response. To pass this to a requests_html.HTMLSession() called session, do as follows:

import requests_html

html_response = requests_html.HTMLResponse._from_response(browser.response, session)

html_response.html.render()  # This now works


来源:https://stackoverflow.com/questions/54215036/combining-robobrowser-with-requests-html

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