效果展示:
下载的PDF文件
PDF文件的内部展示
实现逻辑:
- 使用urllib爬取网页,获取该url的响应内容
- 利用BeautifulSoup选取响应中你需要的节点
- 拼接数据
- 保存HTML文件
- 使用wkhtmltopdf将HTML文件转换为PDF文件
相关代码:
from bs4 import BeautifulSoup #页面解析,获取数据
import urllib.request,urllib.error #指定URL,获取页面数据
import pdfkit
def main():
#1.使用urllib爬取网页,获取该url的响应内容
baseUrl = "https://blog.csdn.net/j1451284189/article/details/122420310"
header = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3776.400 QQBrowser/10.6.4212.400"}
request = urllib.request.Request(url = url,headers=header)
html = ""
try:
response = urllib.request.urlopen(request)
html = response.read().decode("utf-8")
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reson"):
print(e.reson)
#2.利用BeautifulSoup选取响应中你需要的节点
bs = BeautifulSoup(html,"html.parser")
blog_detail = bs.find_all('div', class_="article_content")[0]
file_name = bs.find_all('h1', class_="title-article")[0]
file_name = file_name.text
# 3.拼接数据
html = \
'''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{}
</body>
</html>
'''.format(blog_detail)
# 4.保存HTML文件
try:
with open(r'.\blog\{}.html'.format(file_name), 'w', encoding='utf-8') as f:
f.write(html)
except Exception as e:
print('文件名错误')
# 5.使用wkhtmltopdf将HTML文件转换为PDF文件
try:
config = pdfkit.configuration(wkhtmltopdf=r'D:\LenovoSoftstore\wkhtmltopdf\bin\wkhtmltopdf.exe')
pdfkit.from_file(
'.\\blog\{}.html'.format(file_name),
r'.\blog\{}.pdf'.format(file_name),
configuration=config
)
print(r'--文件下载成功:\blog\{}.pdf'.format(file_name))
except Exception as e:
print(r'--文件转换为PDF失败)
其他
参考:https://blog.csdn.net/weixin_54733110/article/details/117884699(这篇文章讲得挺详细的,我的博客思路是参考本篇文章,我的前面的1-2步实现与这篇博客不同,最后下载下来的PDF文件效果一致)
这个案例是爬取的一篇CSDN博客详情页的博客内容主体,能够成功的原因是因为这个网站是静态的,如果是那种动态的网页,这种方法是行不通的,当然只爬取CSDN博客是可以的。