2

I am trying to addapt the code found in

Python + Selenium + PhantomJS render to PDF

so I instead of saving one web page as a pdf file, I can iterate over a list of urls and save each one with a specific name (from another list).

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

I tested modifying

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

to

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

so as to include the each name in the list using count but have not been successful.

Also tried:

dest = file_user[count]+".pdf"

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

But did not work either.

I greatly appreciate a suggestion for the appropriate syntax.

It must be very simple but I am a noobie.

Community
  • 1
  • 1
Diego
  • 637
  • 3
  • 10
  • 24

1 Answers1

1

Use string formatting:

render = 'this.render("{file_name}.pdf")'.format(file_name=names[count])
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Awesome!! It worked perfectly. Thank you so much!! The link is a fantastic bonus. – Diego Jul 01 '15 at 19:24
  • An addional question...using beautifulsoup I extracted some portions of the html and replaced some text within tags, the issue is that it still renders the original html from the initial link...what do you suggest? Should I post it as a different question? Thanks! – Diego Jul 02 '15 at 21:18
  • @Diego yeah, solving it separately makes sense – alecxe Jul 02 '15 at 21:20