当前位置:   article > 正文

ChatGPT辅助编程,一次有益的尝试

ChatGPT辅助编程,一次有益的尝试

 如果大家想学习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代码如下: 

  1. import requests
  2. from bs4 import BeautifulSoup
  3. import re
  4. from urllib.parse import unquote
  5. import pdfkit
  6. from PyPDF2 import PdfMerger
  7. url = "https://zhuanlan.zhihu.com/p/655702770"
  8. # 发送GET请求并获取页面内容
  9. response = requests.get(url)
  10. html_content = response.text
  11. # 使用BeautifulSoup解析HTML
  12. soup = BeautifulSoup(html_content, 'html.parser')
  13. # 提取所有的超链接
  14. links = soup.find_all('a', href=True)
  15. target_regex = re.compile(r'target=(.+)')
  16. target_html = []
  17. # 输出所有链接
  18. for link in links:
  19. # 获取原始的 href 属性
  20. raw_href = link['href']
  21. # 使用 unquote 处理 URL 编码
  22. decoded_href = unquote(raw_href)
  23. match = target_regex.search(decoded_href)
  24. if match:
  25. target_content = match.group(1)
  26. print(target_content)
  27. target_html.append(target_content)
  28. config = pdfkit.configuration(wkhtmltopdf='/usr/bin/wkhtmltopdf')
  29. output_pdf_path = "output.pdf"
  30. # Create a PDF merger object
  31. pdf_merger = PdfMerger()
  32. # Generate PDFs and add them to the merger
  33. for i, target_url in enumerate(target_html):
  34. pdf_file_path = f"output_{i}.pdf"
  35. pdfkit.from_url(target_url, pdf_file_path, configuration=config)
  36. pdf_merger.append(pdf_file_path)
  37. # Write the combined PDF to the output file
  38. with open(output_pdf_path, 'wb') as output_pdf:
  39. pdf_merger.write(output_pdf)
  40. print(f"Combined PDF saved to {output_pdf_path}")

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/70587
推荐阅读
相关标签
  

闽ICP备14008679号