python学习—保存网页到本地 html及pdf

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_20280061/article/details/51674534

python学习—保存网页到本地 html及pdf

直接上代码:


    # -*- coding: utf-8 -*-
    """
    Created on Tue Jun 14 13:01:58 2016

    @author: 
    """

    import urllib2
    import cookielib
    import pdfkit

    cj = cookielib.LWPCookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)
    url = "https://www.taobao.com/"
    req = urllib2.Request(url)
    ''' 保存html到本地'''
    operate = opener.open(req)
    msg = operate.read()
    document = 'D://1.html'  
    file_ = open(document,'w')   
    file_.write(msg)
    file_.close()

    path_wk = r'C:\Python27\wkhtmltopdf\bin\wkhtmltopdf.exe'
    config = pdfkit.configuration(wkhtmltopdf = path_wk)

    '''保存pdf到本地'''
    pdfkit.from_url(url, r'D:\are you coding\pdf\taobao.pdf', configuration=config)

猜你喜欢

转载自blog.csdn.net/sinat_20280061/article/details/51674534