爬取图片保存为pdf

复制代码
   本文章想借着爬虫给大家介绍一下图片转pdf,有需要的友友们可以看看参考参考,有帮助到友友的可以收藏+关注。下面以爬取初中7年级数学上册为例给大家演示一下。网址是这个    https://mp.weixin.qq.com/s?__biz=MzAxOTE4NjI1Mw==&mid=2650214000&idx=6&sn=2e627183fc9376a2f09f29fb84d912b8&chksm=83c97952b4bef04499f9797b0b01daa54b46d00ef9958c1e521da0a29f41559c99aefe96f157&scene=27

1 导入包

python 复制代码
import requests
from lxml import html,etree
from bs4 import BeautifulSoup
import re
import pandas as pd
from PIL import Image
from io import BytesIO
import os
from PyPDF2 import PdfFileMerger, PdfFileReader
import PyPDF2

2 爬取目标url获取有用照片url

python 复制代码
### 1 解析网页获取可用数据
URL = 'https://mp.weixin.qq.com/s?__biz=MzAxOTE4NjI1Mw==&mid=2650214000&idx=6&sn=2e627183fc9376a2f09f29fb84d912b8&chksm=83c97952b4bef04499f9797b0b01daa54b46d00ef9958c1e521da0a29f41559c99aefe96f157&scene=27'
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
respons = requests.get(url =URL,headers= headers)
respons.encoding='utf-8'
respons = respons.text
soup = BeautifulSoup(respons,'lxml')
# 使用正则表达式查找所有https开头的URL
urls = re.findall(r'https://\S+', str(soup))
book_url = []
# 打印找到的URL
for url in urls:
    if len(url)> 139:
        # print(url)
        book_url.append(url)
    else:
        None

3 爬取url对应的照片

python 复制代码
for index,url_ in enumerate(book_url):
    image_filename = r'E:/学习/7年级上册数学/{}.jpeg'.format(index)
    response = requests.get(url_)
    if response.status_code == 200:
        with open(image_filename, 'wb') as file:
            file.write(response.content)
        print(f'图片已保存到 {image_filename}')
    else:
        print(f'下载失败,状态码: {response.status_code}')

爬到照片:

4 图片转化为pdf并且合并

python 复制代码
### 3将图片转化为pdf
folder_path = r'E:/学习/7年级上册数学'
# 创建一个列表来存储每张图片转换成的PDF文件的路径
pdf_files = []
# 遍历文件夹中的所有文件
for filename in os.listdir(folder_path):
    if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
        file_path = os.path.join(folder_path, filename)
        img = Image.open(file_path)
        # 转换图片为PDF文件,这里假设每张图片保存为一个单独的PDF
        pdf_file_path = f'{folder_path}/temp_{filename.rsplit(".", 1)[0]}.pdf'
        img.save(pdf_file_path, "PDF")
        pdf_files.append(pdf_file_path)
if pdf_files:
    merger = PyPDF2.PdfWriter()
    for pdf in pdf_files:
        merger.append(pdf)
    output_pdf_filename = os.path.join(folder_path, '7年级上册数学.pdf')
    with open(output_pdf_filename, 'wb') as fout:
        merger.write(fout)
    print(f"PDFs merged into {output_pdf_filename}")
else:
    print("No images were found in the specified folder.")

结果如下图:

相关推荐
Dxy123931021616 小时前
Python PDFplumber详解:从入门到精通的PDF处理指南
开发语言·python·pdf
棱镜研途20 小时前
学习笔记丨卷积神经网络(CNN):原理剖析与多领域Github应用
图像处理·笔记·学习·计算机视觉·cnn·卷积神经网络·信号处理
荔枝hu1 天前
springboot生成pdf方案之dot/html/图片转pdf三种方式
spring boot·pdf·html
输出的都是我的1 天前
latex格式中插入eps格式的图像的编译命令
pdf
千宇宙航1 天前
闲庭信步使用图像验证平台加速FPGA的开发:第九课——图像插值的FPGA实现
图像处理·计算机视觉·缓存·fpga开发
FreeBuf_2 天前
微软365 PDF导出功能存在本地文件包含漏洞,可泄露敏感服务器数据
服务器·microsoft·pdf
加油吧zkf2 天前
Conda虚拟环境管理:从入门到精通的常用命令
图像处理·深度学习·计算机视觉·conda
PyAIExplorer2 天前
图像处理中的边缘填充:原理与实践
图像处理·人工智能
熊出没2 天前
Vue前端导出页面为PDF文件
前端·vue.js·pdf
最懒的菜鸟2 天前
MinerU将PDF转成md文件,并分拣图片
人工智能·pdf