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='
Oh yeah!')open_in_browser(new_response)
但是现在我看到在记事本而不是浏览器(在我的windows系统上)中打开的文本使我认为这是一个文本字符串而不是html(即使它有一个外部html标记):
我怎样才能让它工作?在
编辑:
我把密码改成
^{pr2}$
现在得到:TypeError: Unsupported response type: Response
编辑2:
我意识到在我版本的scrapy(1.1)中,源代码是:def open_in_browser(response, _openfunc=webbrowser.open):
"""Open the given response in a local web browser, populating the
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'
repl = '
' % response.urlbody = body.replace(b'
', 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)
我改变了对HTMLresponse的响应,它开始工作了。谢谢你