from selenium import webdriverimport seleniumdef execute(script, args): driver.execute('executePhantomScript', {'script': script, 'args' : args }) driver = webdriver.PhantomJS('phantomjs')# hack while the python interface lagsdriver.command_executor._commands['executePhantomScript'] = ('POST', '/session/$sessionId/phantom/execute')driver.get('https://www.sina.com')# set page format# inside the execution script, webpage is "this"pageFormat = '''this.paperSize = {format: "Legal", orientation: "portrait" };'''execute(pageFormat, [])# render current pagerender = '''this.render("demo.pdf")'''execute(render, [])
phantom.js 下载地址 http://phantomjs.org/download.html