Python + Selenium + PhantomJS 渲染为 PDF - 多个文件

Python + Selenium + PhantomJS render to PDF - multiple files

我正在尝试添加在

中找到的代码

Python + Selenium + PhantomJS render to PDF

所以我不是将一个网页保存为 pdf 文件,而是可以遍历 url 列表并使用特定名称(来自另一个列表)保存每个 url。

count = 0
while count < length:
    def execute(script, args):
        driver.execute('executePhantomScript', {'script': script, 'args' : args })

    driver = webdriver.PhantomJS('phantomjs')

    # hack while the python interface lags
    driver.command_executor._commands['executePhantomScript'] = ('POST',     '/session/$sessionId/phantom/execute')

    driver.get(urls[count])

    # set page format
    # inside the execution script, webpage is "this"
    pageFormat = '''this.paperSize = {format: "A4", orientation: "portrait" };'''
    execute(pageFormat, [])

    # render current page
    render = '''this.render("test.pdf")'''
    execute(render, [])

    count+=1

我测试了修改

render = '''this.render("test.pdf")'''

render = '''this.render(names[count]+".pdf")'''

以便使用计数将每个名称包含在列表中但未成功。

也尝试过:

dest = file_user[count]+".pdf"

render = '''this.render(dest)'''
execute(render, [])

但是也没用。

非常感谢有关适当语法的建议。

一定很简单,但是我是菜鸟

使用string formatting:

render = 'this.render("{file_name}.pdf")'.format(file_name=names[count])