Scrapy - 如何将 html 字符串加载到 open_in_browser 函数中 [英] Scrapy - How to load html string into open_in_browser function
问题描述
我正在处理一些返回 HTML
字符串 (my_html
) 的代码.我想使用 https 在浏览器中查看它的外观://doc.scrapy.org/en/latest/topics/debug.html#open-in-browser.为此,我尝试创建一个主体设置为my_html
"的响应对象.我尝试了很多东西,包括:
new_response = TextResponse(body=my_html)open_in_browser(new_response)
基于响应类(https://doc.scrapy.org/en/latest/topics/request-response.html#response-objects).我得到:
new_response = TextResponse(body=my_html)文件c:\scrapy\http\response\text.py",第 27 行,在 __init__ 中super(TextResponse, self).__init__(*args, **kwargs)类型错误:__init__() 需要至少 2 个参数(给定 2 个)
我怎样才能让它工作?
TextResponse
需要一个 URL 作为第一个参数:
如果你想传递一个正文,你仍然需要一个 URL 作为第一个参数:
<预><代码>>>>scrapy.http.TextResponse(body='<html><body>噢耶!</body></html>')回溯(最近一次调用最后一次):文件<console>",第 1 行,在 <module> 中文件/home/paul/.virtualenvs/scrapy12/local/lib/python2.7/site-packages/scrapy/http/response/text.py",第 27 行,在 __init__super(TextResponse, self).__init__(*args, **kwargs)类型错误:__init__() 需要至少 2 个参数(给定 2 个)>>>scrapy.http.TextResponse('http://www.example.com', body='<html><body>噢耶!</body></html>')<200 http://www.example.com>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. To do this I've tried to create a response object with body set to 'my_html
'. I've tried a bunch of things including:
new_response = TextResponse(body=my_html)
open_in_browser(new_response)
based on the response class (https://doc.scrapy.org/en/latest/topics/request-response.html#response-objects). I'm getting:
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)
How can I get this working?
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 2 arguments (2 given)
>>> scrapy.http.TextResponse('http://www.example.com', body='<html><body>Oh yeah!</body></html>')
<200 http://www.example.com>
这篇关于Scrapy - 如何将 html 字符串加载到 open_in_browser 函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!