Skip to content Skip to sidebar Skip to footer

Scrapy - How To Load Html String Into Open_in_browser Function

I am working on some code which returns an HTML string (my_html). I want to see how this looks in a browser using https://doc.scrapy.org/en/latest/topics/debug.html#open-in-browser

Solution 1:

TextResponse expects a URL as first argument:

>>>scrapy.http.TextResponse('http://www.example.com')
<200 http://www.example.com>
>>>

If you want to pass a body, you still need a URL as first argument:

>>> scrapy.http.TextResponse(body='<html><body>Oh yeah!</body></html>')
Traceback (most recent call last):
  File"<console>", line 1, in <module>
  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 2arguments (2 given)
>>> scrapy.http.TextResponse('http://www.example.com', body='<html><body>Oh yeah!</body></html>')
<200http://www.example.com>

Solution 2:

Your error seems to be with the TextResponse initialization, according to the docs, you need to initialize it with a URL, TextResponse("http://www.expample.com") should do it.

It looks like you are looking at the Response object docs and trying to use TextResponse like you would Response, by the looks of your optional argument and link to the docs.

Post a Comment for "Scrapy - How To Load Html String Into Open_in_browser Function"