Scrapy opening html in editor , not browser
<p><a href="https://i.stack.imgur.com/LWxmP.png" rel="nofollow"><img src="https://i.stack.imgur.com/LWxmP.png" alt="enter image description here"></a></p>

<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>.  I just asked a question on this (<a href="http://stackoverflow.com/questions/40109782/scrapy-how-to-load-html-string-into-open-in-browser-function">Scrapy - How to load html string into open_in_browser function</a>) and the answers have shown me how to load the string into the 'open_in_browser object'</p>

<pre><code>headers = {
        'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        'upgrade-insecure-requests': "1",
        'user-agent': "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36",

        'referer': "http://civilinquiry.jud.ct.gov/GetDocket.aspx",
        'accept-encoding': "gzip, deflate, sdch",
        'accept-language': "en-US,en;q=0.8",
        'cache-control': "no-cache",
            }

    new_response = TextResponse('https://doc.scrapy.org/en/latest/topics/request-response.html#response-objects', headers=headers, body='&lt;html&gt;&lt;body&gt;Oh yeah!&lt;/body&gt;&lt;/html&gt;')
    open_in_browser(new_response)
</code></pre>

<p>However now I'm seeing the text open up in notepad rather than the browser (on my windows system) making me think that the system thinks this is a text string not html (even though it has an outer html tag):</p>

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

<p>edit:</p>

<p>I changed the code to </p>

<pre><code>new_response = Response('https://doc.scrapy.org/en/latest/topics/request-response.html#response-objects', headers=headers, body='&lt;html&gt;&lt;body&gt;Oh yeah!&lt;/body&gt;&lt;/html&gt;')
</code></pre>

<p>Now getting:</p>

<pre><code>TypeError: Unsupported response type: Response
</code></pre>

<p>edit2:</p>

<p>I realized that in my version of scrapy (1.1) the source is:</p>

<pre><code>def open_in_browser(response, _openfunc=webbrowser.open):
    """Open the given response in a local web browser, populating the &lt;base&gt;
    tag for external links to work
    """
    from scrapy.http import HtmlResponse, TextResponse
    # XXX: this implementation is a bit dirty and could be improved
    body = response.body
    if isinstance(response, HtmlResponse):
        if b'&lt;base' not in body:
            repl = '&lt;head&gt;&lt;base href="%s"&gt;' % response.url
            body = body.replace(b'&lt;head&gt;', to_bytes(repl))
        ext = '.html'
    elif isinstance(response, TextResponse):
        ext = '.txt'
    else:
        raise TypeError("Unsupported response type: %s" %
                        response.__class__.__name__)
    fd, fname = tempfile.mkstemp(ext)
    os.write(fd, body)
    os.close(fd)
    return _openfunc("file://%s" % fname)
</code></pre>

<p>I changed Response to HTMLresponse and it started working. Thank you</p>

<p>Look at how the <a href="https://github.com/scrapy/scrapy/blob/129421c7e31b89b9b0f9c5f7d8ae59e47df36091/scrapy/utils/response.py#L63" rel="nofollow"><code>open_in_browser()</code></a> function is defined:</p>

<pre><code>if isinstance(response, HtmlResponse):
    if b'&lt;base' not in body:
        repl = '&lt;head&gt;&lt;base href="%s"&gt;' % response.url
        body = body.replace(b'&lt;head&gt;', to_bytes(repl))
    ext = '.html'
elif isinstance(response, TextResponse):
    ext = '.txt'
else:
    raise TypeError("Unsupported response type: %s" %
                    response.__class__.__name__)
</code></pre>

<p>It would create a <code>.txt</code> file if it is getting a <code>TextResponse</code> - that's why you see the notepad opening the file with your HTML inside. </p>

<p>Instead, you need to initialize the regular <a href="https://doc.scrapy.org/en/latest/topics/request-response.html#scrapy.http.Response" rel="nofollow"><code>scrapy.Response</code></a> object and pass it to <code>open_in_browser()</code>.</p>

<p>Or, you can create the temp HTML file with the desired contents manually and then using the <code>file://</code> protocol open it in a default browser through the <a href="https://docs.python.org/2/library/webbrowser.html#webbrowser.open" rel="nofollow"><code>webbrowser.open()</code></a>. </p>

python scrapy