使用webdriver + phantomjs + pdfkit 生成PDF文件

实例

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Created on Dec 6, 2013

@author: Jay <smile665@gmail.com>
@description: use PhantomJS to parse a web page to get the geo info of an IP
'''
import datetime
import urllib
# from pyquery import PyQuery as pq
import pdfkit

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait  # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC  # available since
import time

#import db
#from db import exec_sql, fetchone_sql, fetchall_sql

import sys
reload(sys)
sys.setdefaultencoding('utf-8')


def spider_question(url):
    """
    功能说明:根据URL生成PDF
    """
    browser = webdriver.PhantomJS(executable_path='./phantomjs')  # 这要可能需要制定phatomjs可执行文件的位置
    # browser.set_window_size(1024, 786)
    print 'start request url', datetime.datetime.now()
    browser.get(url)  # Load page
    print 'end request url', datetime.datetime.now()
    try:
        WebDriverWait(browser, 60).until(EC.title_contains("complete"))  # 查找数据,500毫秒查找一次,找到则继续执行下面代码,超过10秒抛出异常
    except Exception, e:
        print "http 500"
        print e
        browser.quit()
        return "response_500"
    print "end math load:", datetime.datetime.now()
    html = browser.find_element_by_tag_name("html").get_attribute("innerHTML")
    browser.quit()
    html = '<!DOCTYPE html><html>' + html + "</html>"

    print 'begin pdfkit:', datetime.datetime.now()
    pdf_name = "test2.pdf"
    pdfkit.from_string(html, pdf_name)
    print "end pdfkit", datetime.datetime.now()

    return html


def main():
    #url = "http://192.168.0.126/tea/test/wrongset/download/question/?day_list=20151103&user_id=849127&js=1"
    url = "http://192.168.0.126/open/math2_preview/?js=1&engine=webkit2&css=0"
    now = datetime.datetime.now()
    spider_question(url)
    now2 = datetime.datetime.now()
    print(now2-now)
    # pdfkit.from_string("hello", 'test.pdf')

if __name__ == "__main__":
    main()
    print "completed"

 

转载于:https://www.cnblogs.com/weiok/p/5110069.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值