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

#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)
comments powered by Disqus