python下载网页上的pdf文件_利用Python把网页内容转换为pdf格式文件,批量下载到本地!...

import win32api

import win32con

import requests

from bs4 import BeautifulSoup

import webbrowser

import tkinter

from tkinter import filedialog

import pdfkit

# 预览

def take_body():

global url

global body_class

global headers

url = var_url.get()

body_class = var_body.get()

# 请求URL

headers = {

'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',

'Accept-Encoding':'gzip, deflate, br',

'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36'

}

r = requests.get(url,headers = headers)

soup = BeautifulSoup(r.content,'html.parser')

# 提取标题

global title

title = soup.title.text

# 判断该网站是否已下载转换过

f = open('temp.txt','r')

a = f.read()

dict = eval(a)

f.close()

if url.split('/')[2] in dict:

body_class = dict[url.split('/')[2]]

elif body_class == '':

win32api.MessageBox(0, '未从在该网站下载过文档,添加提取条件', '提示', win32con.MB_OK)

# 提取正文

if body_class != '':

body = soup.find_all(class_= body_class)[0]

# 对提取的内容,调用浏览器进行预览

html_test = str(body)

with open('html_test.html','wb')as f:

f.write(html_test.encode('utf-8'))

webbrowser.open('E:PythonHtmlToPdfHtmlToPdf_V0.7html_test.html',new=0,autoraise=True)

# html转pdf

def htmltopdf():

file_name = title + '.pdf'

types = [('pdf文件', '*.pdf')]

dest_dir = filedialog.asksaveasfilename(initialfile = file_name,filetypes = types)

options = {'encoding': 'utf-8'}

pdfkit.from_file('html_test.html', dest_dir, options=options)

dict = {}

f = open('temp.txt','r')

a = f.read()

dict = eval(a)

f.close()

dict[url.split('/')[2]] = body_class

f = open('temp.txt','w')

f.write(str(dict))

f.close()

# 创建窗口

root = tkinter.Tk()

root.title('HtmlToPdf')

root.geometry('300x200')

var_url = tkinter.StringVar()

var_body = tkinter.StringVar()

label_1 = tkinter.Label(root,text = 'URL:').place(x=10,y=10)

text_1 = tkinter.Entry(root,textvariable = var_url).place(x=100,y=10)

label_2 = tkinter.Label(root,text = '输入提取条件:').place(x=10,y=55)

text_2 = tkinter.Entry(root,show = None,textvariable = var_body).place(x=100,y=55)

button_2 = tkinter.Button(root,text = '预览',command = take_body).place(x=250,y=50)

label_3 = tkinter.Label(root,text = '是否打印:').place(x=10,y=100)

button_3 = tkinter.Button(root,text = '是',command = htmltopdf).place(x=100,y=95)

button_3 = tkinter.Button(root,text = '否',command = root.quit).place(x=130,y=95)

root.mainloop()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值