我正在准备考试,这些网站会定期上传时事。
我不想每天重新打开这些多个网站,而是想简化这个过程,将这些网页下载为 pdf 格式并将这些 pdf 合并为一个,所以最后我必须阅读一个 pdf(而不是点击千需要阅读 3 页)。
我确实尝试过使用 python 和 powershell 寻找选项,但这些方法并不是在每个网站上都有效,我必须自己找到(并非在每个页面上都找到)并更改网页链接(这不计入自动化) ,我想),由于我不喜欢编程,所以我只能从互联网上复制粘贴东西,这是行不通的。
所以我正在寻找自动化脚本; •根据日期,每天从所有网站(如下所示)中获取最新时事的合并单一 pdf 文件。
• 如果一种方法不适用于所有站点,我可以运行多个脚本(这个旧盒子有时会遇到麻烦)
• 在某些站点,预赛和主要时事是分开的,如果您也能做到这一点,我们将不胜感激。
• 请解释这些步骤,以便我将来可以在需要时在其他网站上重新创建此内容
网站:[[ https://vajiramandravi.com/upsc-daily-current-affairs/
https://visionias.in/resources/daily_current_affairs.php?type=1
https://www.drishtiias.com/current-affairs-news-analysis-editorials
https://forumias.com/blog/subjectwise-current-affairs-for-upsc-ias-prelims-examination/
https://iasbaba.com/current-affairs-for-ias-upsc-exams/。 ]]
谢谢。
PS:我没有 24x7 的互联网连接,因此我只能在给定的允许持续时间内运行脚本。
要将每日更新的网页自动转换为单个 PDF,您需要一个脚本:
考虑到您的限制,让我们使用以下工具构建基于 Python 的解决方案:
requests
用于获取网页内容。pdfkit
将 HTML 转换为 PDF。PyPDF2
合并 PDF。pdfkit
和 PyPDF2
软件包:
pip install pdfkit PyPDF2 requests
wkhtmltopdf
(由 pdfkit
使用):
brew install wkhtmltopdf
)。sudo apt-get install wkhtmltopdf
)。这是一个简单的 Python 脚本:
import requests
import pdfkit
import os
from PyPDF2 import PdfFileMerger
from datetime import datetime
# URLs of the webpages to fetch
webpages = [
"https://vajiramandravi.com/upsc-daily-current-affairs/",
"https://visionias.in/resources/daily_current_affairs.php?type=1",
"https://www.drishtiias.com/current-affairs-news-analysis-editorials",
"https://forumias.com/blog/subjectwise-current-affairs-for-upsc-ias-prelims-examination/",
"https://iasbaba.com/current-affairs-for-ias-upsc-exams/"
]
# Create a directory to store the PDFs if it doesn't exist
pdf_directory = "daily_pdfs"
if not os.path.exists(pdf_directory):
os.makedirs(pdf_directory)
# List to store paths of generated PDFs
pdf_files = []
# Convert each webpage to a PDF and store the filename
for idx, url in enumerate(webpages):
pdf_filename = f"{pdf_directory}/webpage_{idx+1}.pdf"
pdfkit.from_url(url, pdf_filename)
pdf_files.append(pdf_filename)
# Merge the PDFs into one
merger = PdfFileMerger()
for pdf_file in pdf_files:
merger.append(pdf_file)
# Save the merged PDF with today's date
today = datetime.now().strftime("%Y-%m-%d")
merged_pdf_filename = f"{pdf_directory}/current_affairs_{today}.pdf"
merger.write(merged_pdf_filename)
merger.close()
print(f"Merged PDF saved as: {merged_pdf_filename}")
convert_webpages.py
。python convert_webpages.py
运行脚本。daily_pdfs
目录中,文件名中包含当前日期。pdfkit
引发错误,请检查 wkhtmltopdf
是否已安装并且位于您的 PATH 中。pdfkit
可能无法正确呈现。使用此脚本,您可以获取多个网页并将其转换为合并的 PDF。它可以通过更新
webpages
列表来适应其他 URL。