2013-08-05 7 views
6

Sto usando selenio/phantomjs per creare file png di html in python. C'è un modo per generare il png da una stringa html o da un filehandle (invece di un sito Web)? Ho cercato tra i documenti del selenio e ho cercato su Google ma non ho trovato risposta. Ho:Come posso generare un file png con selenio/phantomjs da una stringa?

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

Hai provato 'file: /// PercorsoFile/tmp.html'? –

+0

Questo mi dà anche un file png vuoto. È documentato da qualche parte? –

+0

No. Non ho mai usato PhantomJS, ma il file: /// è ciò che i browser usano per andare a un file. Non ho sfuggito alla riga sopra (e ora non posso modificarlo) ... ti sei assicurato che fuggisse? –

risposta

11

PhantomJS

var page = require('webpage').create(); 
page.open('http://github.com/', function() { 
    page.render('github.png'); 
    phantom.exit(); 
}); 

Questo è come ottenere uno screenshot in phantomJS, ho usato phantomJS da qualche tempo.

È possibile trovare maggiori informazioni here.

Selenio

driver = webdriver.Chrome(); 
driver.get('http://www.google.com'); 
driver.save_screenshot('out.png'); 
driver.quit(); 

Spero che questo aiuti.

+0

È necessario rimuovere il punto e virgola dalla sezione Selenium (Python). – man2xxl

+0

Non è necessario –

2

PhantomJS

var page = require('webpage').create(); 
page.content = '<html><body><p>Hello world</p></body></html>'; 
page.render('name.png'); 

si imposta il contenuto della pagina utilizzando page.content Poi si esegue il rendering utilizzando page.render

Example using phantomjs-node

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(); 
     }); 
    }); 
}); 
4

puro buon vecchio pitone - insieme t si accontenta di qualsiasi pagina aperta al tuo html di destinazione - tramite JS. Prendendo il vostro codice di esempio:

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" 
1

Sembra che le linee

f = open(myFile,'w') 
f.write(htmlString) 

sono problematici, come il file generato è vuoto.

Ho risolto questo problema con

with open(myFile,'wt') as f: 
    f.write(htmlString) 

o potrebbe essere necessario aggiungere un

f.close() to your code 
Problemi correlati