Python 在编辑器中而不是浏览器中打开html
我正在编写一些返回Python 在编辑器中而不是浏览器中打开html,python,scrapy,Python,Scrapy,我正在编写一些返回HTML字符串(myu HTML)的代码。我想看看它在浏览器中的外观。我刚才问了一个问题(),答案向我展示了如何将字符串加载到“open_in_browser对象”中 headers = { 'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 'upgrade-insecure-requests': "1",
HTML
字符串(myu HTML
)的代码。我想看看它在浏览器中的外观。我刚才问了一个问题(),答案向我展示了如何将字符串加载到“open_in_browser对象”中
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='<html><body>Oh yeah!</body></html>')
open_in_browser(new_response)
编辑2:
我意识到在我的scrapy(1.1)版本中,源代码是:
def open_in_browser(response, _openfunc=webbrowser.open):
"""Open the given response in a local web browser, populating the <base>
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'<base' not in body:
repl = '<head><base href="%s">' % response.url
body = body.replace(b'<head>', 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)
def open_在浏览器中(响应,_openfunc=webbrowser.open):
“”“在本地web浏览器中打开给定响应,填充
用于工作的外部链接的标记
"""
从scrapy.http导入HtmlResponse,TextResponse
#XXX:这个实现有点脏,可以改进
body=response.body
如果存在(响应、HtmlResponse):
如果b'查看函数是如何定义的:
if isinstance(response, HtmlResponse):
if b'<base' not in body:
repl = '<head><base href="%s">' % response.url
body = body.replace(b'<head>', to_bytes(repl))
ext = '.html'
elif isinstance(response, TextResponse):
ext = '.txt'
else:
raise TypeError("Unsupported response type: %s" %
response.__class__.__name__)
如果存在(响应、HtmlResponse):
如果b'这在Python 2.7中对我有效
open_in_browser(scrapy.http.HtmlResponse(url = '', body = bytes(response.body)))
- 我尝试不使用url,也不将response.body转换为字节,但在这两种情况下都会抛出错误
- 如果您使用scrapy.http.Response而不是scrapy.http.HtmlResponse,它也会抱怨不支持的响应类型
看起来可能取决于.html
文件的文件扩展名关联。您可以尝试将Windows中的文件关联更改为将.html
关联到浏览器应用程序。
if isinstance(response, HtmlResponse):
if b'<base' not in body:
repl = '<head><base href="%s">' % response.url
body = body.replace(b'<head>', to_bytes(repl))
ext = '.html'
elif isinstance(response, TextResponse):
ext = '.txt'
else:
raise TypeError("Unsupported response type: %s" %
response.__class__.__name__)
open_in_browser(scrapy.http.HtmlResponse(url = '', body = bytes(response.body)))