python 保存网页为pdf到本地

时间:2024-02-24 13:49:00
 1 import urllib2
 2     import cookielib
 3     import pdfkit
 4     
 5     cj = cookielib.LWPCookieJar()
 6     opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
 7     urllib2.install_opener(opener)
 8     url = "https://www.taobao.com/"
 9     req = urllib2.Request(url)
10     \'\'\' 保存html到本地\'\'\'
11     operate = opener.open(req)
12     msg = operate.read()
13     document = \'D://1.html\'  
14     file_ = open(document,\'w\')   
15     file_.write(msg)
16     file_.close()
17     
18     path_wk = r\'C:\Python27\wkhtmltopdf\bin\wkhtmltopdf.exe\'
19     config = pdfkit.configuration(wkhtmltopdf = path_wk)
20     
21     \'\'\'保存pdf到本地\'\'\'
22     pdfkit.from_url(url, r\'D:\are you coding\pdf\taobao.pdf\', configuration=config)