Python 如何从字符串生成带有 selenium/phantomjs 的 png 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18067021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I generate a png file w/ selenium/phantomjs from a string?
提问by
I am using selenium/phantomjs to create png files of html in python. Is there a way to generate the png from an html string or filehandle (instead of a website)? I've searched through the selenium docs and googled but couldn't find an answer. I have:
我正在使用 selenium/phantomjs 在 python 中创建 html 的 png 文件。有没有办法从 html 字符串或文件句柄(而不是网站)生成 png?我搜索了硒文档并用谷歌搜索但找不到答案。我有:
htmlString = '<html><body><div style="background-color:red;height:500px;width:500px;">This is a png</div></body></html>'
myFile = 'tmp.html'
f = open(myFile,'w')
f.write(htmlString)
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.set_window_size(1024, 768)
#driver.get('https://google.com/') # this works fine
driver.get(myFile) # passing the file name or htmlString doesn't work...creates a blank png with nothing
driver.save_screenshot('screen.png')
driver.quit()
print "png file created"
采纳答案by coding_idiot
PhantomJS
PhantomJS
var page = require('webpage').create();
page.open('http://github.com/', function () {
page.render('github.png');
phantom.exit();
});
This is how to get a screenshot in phantomJS, I've used phantomJS for some time now.
这是如何在 phantomJS 中获取屏幕截图,我已经使用 phantomJS 一段时间了。
You can find more information here.
Selenium
硒
driver = webdriver.Chrome();
driver.get('http://www.google.com');
driver.save_screenshot('out.png');
driver.quit();
Hope this helps.
希望这可以帮助。
回答by rahulroy9202
PhantomJS
PhantomJS
var page = require('webpage').create();
page.content = '<html><body><p>Hello world</p></body></html>';
page.render('name.png');
You set the content of the page using page.content
Then you render it using page.render
您使用设置页面的内容page.content
然后使用page.render
phantom.create(function (ph) {
ph.createPage(function (page) {
page.set('viewportSize', {width:1440,height:900})
//like this
page.set('content', html);
page.render(path_to_pdf, function() {
//now pdf is written to disk.
ph.exit();
});
});
});
回答by Todor Minakov
Pure good old python - set the content on any opened page to your target html - through JS. Taking your example code:
纯粹的好老 python - 通过 JS 将任何打开的页面上的内容设置为目标 html。以您的示例代码为例:
from selenium import webdriver
htmlString = '<html><body><div style="background-color:red;height:500px;width:500px;">This is a png</div></body></html>'
driver = webdriver.PhantomJS() # the normal SE phantomjs binding
driver.set_window_size(1024, 768)
driver.get('https://google.com/') # whatever reachable url
driver.execute_script("document.write('{}');".format(htmlString)) # changing the DOM
driver.save_screenshot('screen.png') #screen.png is a big red rectangle :)
driver.quit()
print "png file created"
回答by Thierry Barnier
It seems the lines
似乎线条
f = open(myFile,'w')
f.write(htmlString)
Are problematic, as the generated file is empty.
有问题,因为生成的文件是空的。
I fixed this issue with
我解决了这个问题
with open(myFile,'wt') as f:
f.write(htmlString)
or you might have to add a
或者你可能需要添加一个
f.close() to your code