2013-08-05 28 views
6

Tôi đang sử dụng selenium/phantomjs để tạo tệp png của html trong python. Có cách nào để tạo ra png từ một chuỗi html hoặc filehandle (thay vì một trang web)? Tôi đã tìm kiếm thông qua các tài liệu selen và googled nhưng không thể tìm thấy một câu trả lời. Tôi có:Làm cách nào để tạo tệp png w/selenium/phantomjs từ chuỗi?

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

bạn đã thử file ': /// PathToFile/tmp.html'? –

+0

Điều đó cũng cho tôi một tệp png trống. Đó có phải là tài liệu ở bất cứ đâu không? –

+0

No. Tôi chưa bao giờ sử dụng PhantomJS, nhưng tập tin: /// là trình duyệt sử dụng để đi đến một tệp. Tôi đã không thoát khỏi dòng trên (và bây giờ tôi không thể chỉnh sửa nó) ... bạn đã chắc chắn rằng nó thoát? –

Trả lời

11

PhantomJS

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

Đây là cách để có được một ảnh chụp màn hình trong phantomJS, tôi đã sử dụng phantomJS một thời gian bây giờ.

Bạn có thể tìm thêm thông tin here.

Selenium

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

Hope this helps.

+0

Bạn nên xóa dấu chấm phẩy của mình khỏi phần Selenium (Python). – man2xxl

+0

Bạn không cần phải –

2

PhantomJS

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

Bạn đặt nội dung của trang bằng cách sử dụng page.content Sau đó, bạn làm cho nó sử dụng 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

tinh khiết python cũ tốt - bộ t ông nội dung trên bất kỳ trang đã mở nào tới html mục tiêu của bạn - thông qua JS. Lấy ví dụ mã của bạn:

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

Có vẻ như các dòng

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

Are có vấn đề, như các tập tin được tạo rỗng.

Tôi cố định vấn đề này với

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

hoặc bạn có thể phải thêm một

f.close() to your code 
Các vấn đề liên quan