爬取图片保存为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.")

结果如下图:

相关推荐
weixin_397574093 天前
PDF复杂表格的1:1还原引擎:跨页表格自动拼接技术实战
大数据·人工智能·pdf
Metaphor6923 天前
使用 Python 将 PDF 转换为 HTML
python·pdf·html
2601_961845153 天前
粉笔行测5000题电子版|pdf|解析
pdf·新媒体运营·github·个人开发·内容运营·规格说明书·极限编程
Sour3 天前
PDF翻译卡住不动怎么办?扫描件、OCR 和大文件排查清单
前端·pdf·ocr
狂奔solar3 天前
OpenDataLoader-PDF 做 PDF 解析可视化调试器
pdf·rag 预处理
chatexcel3 天前
ChatExcel Max使用教程:图片、PDF、网页与复杂Excel的一站式数据分析
数据分析·pdf·excel
绘梨衣5473 天前
PDF表格解析知识总结
开发语言·python·pdf
qq_546937273 天前
Excel批量转PDF_Word_图片,支持自动合并报表,效率翻倍。
pdf·word·excel
sali-tec3 天前
C# 基于OpenCv的视觉工作流-章85-包胶不良检测
图像处理·人工智能·opencv·算法·计算机视觉
zyplayer-doc3 天前
企业知识库安全与权限管理完全指南:从加密到审计的六层防护
人工智能·安全·pdf·编辑器·创业创新