Scrapy - How to load html string into open_in_browser function
<p>I am working on some code which returns an <code>HTML</code> string (<code>my_html</code>). I want to see how this looks in a browser using <a href="https://doc.scrapy.org/en/latest/topics/debug.html#open-in-browser" rel="nofollow">https://doc.scrapy.org/en/latest/topics/debug.html#open-in-browser</a>. To do this I've tried to create a response object with body set to '<code>my_html</code>'. I've tried a bunch of things including:</p>

<pre><code>new_response = TextResponse(body=my_html)
open_in_browser(new_response)
</code></pre>

<p>based on the response class (<a href="https://doc.scrapy.org/en/latest/topics/request-response.html#response-objects" rel="nofollow">https://doc.scrapy.org/en/latest/topics/request-response.html#response-objects</a>). I'm getting:</p>

<pre><code>new_response = TextResponse(body=my_html)
  File "c:\scrapy\http\response\text.py", line 27, in __init__
    super(TextResponse, self).__init__(*args, **kwargs)
TypeError: __init__() takes at least 2 arguments (2 given)
</code></pre>

<p>How can I get this working?</p>

<p><a href="https://docs.scrapy.org/en/latest/topics/request-response.html#scrapy.http.TextResponse" rel="nofollow"><code>TextResponse</code> expects a URL as first argument</a>:</p>

<pre><code>&gt;&gt;&gt; scrapy.http.TextResponse('http://www.example.com')
&lt;200 http://www.example.com&gt;
&gt;&gt;&gt; 
</code></pre>

<p>If you want to pass a body, you still need a URL as first argument:</p>

<pre><code>&gt;&gt;&gt; scrapy.http.TextResponse(body='&lt;html&gt;&lt;body&gt;Oh yeah!&lt;/body&gt;&lt;/html&gt;')
Traceback (most recent call last):
  File "&lt;console&gt;", line 1, in &lt;module&gt;
  File "/home/paul/.virtualenvs/scrapy12/local/lib/python2.7/site-packages/scrapy/http/response/text.py", line 27, in __init__
    super(TextResponse, self).__init__(*args, **kwargs)
TypeError: __init__() takes at least 2 arguments (2 given)
&gt;&gt;&gt; scrapy.http.TextResponse('http://www.example.com', body='&lt;html&gt;&lt;body&gt;Oh yeah!&lt;/body&gt;&lt;/html&gt;')
&lt;200 http://www.example.com&gt;
</code></pre>

python scrapy