2016-10-18 80 views
0

我正在處理一些返回HTML字符串(my_html)的代碼。我想看看如何在瀏覽器中使用https://doc.scrapy.org/en/latest/topics/debug.html#open-in-browser。爲此,我嘗試創建一個響應對象,其主體設置爲'my_html'。我已經嘗試了一堆東西,包括:Scrapy - 如何將HTML字符串加載到open_in_browser函數

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) 
    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) 

我該如何得到這個工作?

+0

* ...至少需要2個參數(2給出)* WTF? – linusg

回答

1

TextResponse expects a URL as first argument

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

如果你想傳遞一個肉體,你依然需要一個URL作爲第一個參數:

>>> 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> 
+0

謝謝,我試圖更好地檢查文檔,因此你需要關於URL的觀點(class scrapy.http.Response(url [,status = 200,headers = None,body = b'',flags =無,請求=無]))是我未來尋找的東西。 – user61629

1

你的錯誤似乎與TextResponse初始化,according to the docs,你需要初始化它與一個URL,TextResponse("http://www.expample.com")應該這樣做。

看起來您正在查看Response對象文檔,並嘗試使用TextResponse,就像您的可選參數的外觀和鏈接到文檔一樣,您將使用。

+0

感謝您的幫助。 – user61629

相關問題