ChatGPT辅助编程,一次有益的尝试(网页到pdf转换)

 如果大家想学习PCIe,搜索网上的信息,大概率会看到chinaaet上Felix的PCIe扫盲系列的博文

Felix-PCIe扫盲

每次看这个系列博文的时候,我都在想有没有什么方法可以把这个系列的博文都保存到一个pdf文件中,这样方便阅读。于是有了下面使用ChatGPT进行辅助编程的内容:

我是使用知乎上的引用Felix博文网页为根开始工作的:

PCIe扫盲系列博文

下面的链接是我和ChatGPT对话的详细内容: 

Convert webpage to PDF.icon-default.png?t=N7T8https://chat.openai.com/share/812bae9d-76bb-4701-8379-cd200aa45ad0

生成的可用python代码如下: 

import requests
from bs4 import BeautifulSoup
import re
from urllib.parse import unquote
import pdfkit
from PyPDF2 import PdfMerger


url = "https://zhuanlan.zhihu.com/p/655702770"

# 发送GET请求并获取页面内容
response = requests.get(url)
html_content = response.text

# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(html_content, 'html.parser')

# 提取所有的超链接
links = soup.find_all('a', href=True)
target_regex = re.compile(r'target=(.+)')

target_html = []

# 输出所有链接
for link in links:
    # 获取原始的 href 属性
    raw_href = link['href']

    # 使用 unquote 处理 URL 编码
    decoded_href = unquote(raw_href)

    match = target_regex.search(decoded_href)

    if match:
        target_content = match.group(1)
        print(target_content)
        target_html.append(target_content)

config = pdfkit.configuration(wkhtmltopdf='/usr/bin/wkhtmltopdf')

output_pdf_path = "output.pdf"

# Create a PDF merger object
pdf_merger = PdfMerger()

# Generate PDFs and add them to the merger
for i, target_url in enumerate(target_html):
    pdf_file_path = f"output_{i}.pdf"
    pdfkit.from_url(target_url, pdf_file_path, configuration=config)
    pdf_merger.append(pdf_file_path)

# Write the combined PDF to the output file
with open(output_pdf_path, 'wb') as output_pdf:
    pdf_merger.write(output_pdf)

print(f"Combined PDF saved to {output_pdf_path}")

这样生成的pdf是将网页内容全部照搬到pdf的,下图红框中的内容其实都是不需要

 其实pdfkit从网页生成pdf的时候,是支持自定义css的,我们可以在浏览器上F12,然后自定义一些CSS样式,把不需要的内容不让它显示就行,然后正文可以铺满整个页面

pdfkit.css文件内容如下:

.wrapper, .wrapper.grey, .grid-100, .row.grid-100.copyright, .sidebar {
    display: none;
}

.grid.new-ui {
    display: flex;
}

.maintext {
    flex-grow: 1;
}

div#fixedbutton {
    display: none;
}

然后修改python代码如下:

import requests
from bs4 import BeautifulSoup
import re
from urllib.parse import unquote
import pdfkit
from PyPDF2 import PdfMerger
 
 
url = "https://zhuanlan.zhihu.com/p/655702770"
 
# 发送GET请求并获取页面内容
response = requests.get(url)
html_content = response.text
 
# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(html_content, 'html.parser')
 
# 提取所有的超链接
links = soup.find_all('a', href=True)
target_regex = re.compile(r'target=(.+)')
 
target_html = []
 
# 输出所有链接
for link in links:
    # 获取原始的 href 属性
    raw_href = link['href']
 
    # 使用 unquote 处理 URL 编码
    decoded_href = unquote(raw_href)
 
    match = target_regex.search(decoded_href)
 
    if match:
        target_content = match.group(1)
        print(target_content)
        target_html.append(target_content)
 
options = {
    'user-style-sheet': '/home/zhajio/pdfkit.css'
}

config = pdfkit.configuration(wkhtmltopdf='/usr/bin/wkhtmltopdf')
 
output_pdf_path = "output.pdf"
 
# Create a PDF merger object
pdf_merger = PdfMerger()
 
# Generate PDFs and add them to the merger
for i, target_url in enumerate(target_html):
    pdf_file_path = f"output_{i}.pdf"
    pdfkit.from_url(target_url, pdf_file_path, configuration=config, options=options)
    pdf_merger.append(pdf_file_path)
 
# Write the combined PDF to the output file
with open(output_pdf_path, 'wb') as output_pdf:
    pdf_merger.write(output_pdf)
 
print(f"Combined PDF saved to {output_pdf_path}")

这样生成的pdf文件就比较干净了:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值