吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6756|回复: 51
收起左侧

[Python 转载] 文泉下载后pdf生成和目录的获取by_Python

[复制链接]
858043016 发表于 2020-2-12 18:51
本帖最后由 858043016 于 2020-2-20 12:22 编辑

1.获取书名和目录
网页分别是
名称
https://lib-nuanxin.wqxuetang.com/v1/book/initbook?bid=3206295
目录
https://lib-nuanxin.wqxuetang.com/v1/book/catatree?bid=3206295
返回格式是json
2.pymupdf
生成PDF以及添加目录
3.代码
3.1只生成目录,结合FreePic2PDF使用,已经生成utf-16文件了,复制整个文件就可以使用
[Python] 纯文本查看 复制代码
import requests as req
import json
import os
def get_cata(book_id):
    cata=""
    url_name = "https://lib-nuanxin.wqxuetang.com/v1/book/catatree?bid={}".format(book_id)
    response = req.post(url=url_name, headers=headers)
    book_cata = json.loads(response.text)
    for i in book_cata['data']:
        cata=cata+str(i['label'])+'\t'+str(i['pnum'])+'\n'
        # print(i['level'],i['label'],i['pnum'])
        if ('children' in i):
            for j in (i['children']):
                # print("\t",j['level'],j['label'],j['pnum'])
                cata=cata+'\t'+str(j['label'])+'\t'+str(j['pnum'])+'\n'
    return cata

with open('Cookies.txt', 'r') as f:
    cookies=f.read()
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    'Cookie': cookies,
    'Host': 'lib-nuanxin.wqxuetang.com',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'none',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}
book_id=input('输入书的id:\n')
with open('FreePic2Pdf_bkmk.txt', 'w',encoding='utf-16') as f:
    cookies=f.write(get_cata(book_id))

3.2生成PDF和目录
[Python] 纯文本查看 复制代码
import fitz
import requests as req
import json
import os
import img2pdf

def get_name(book_id):
    url_name="https://lib-nuanxin.wqxuetang.com/v1/book/initbook?bid={}".format(book_id)
    response = req.post(url=url_name, headers=headers)
    book_more=json.loads(response.text)
    return(book_more['data']['name'],book_more['data']['pages'])
def get_cata(book_id):
    cata=[]
    url_name = "https://lib-nuanxin.wqxuetang.com/v1/book/catatree?bid={}".format(book_id)
    response = req.post(url=url_name, headers=headers)
    book_cata = json.loads(response.text)
    for i in book_cata['data']:
        cata.append([int(i['level']), i['label'], int(i['pnum'])])
        # print(i['level'],i['label'],i['pnum'])
        if ('children' in i):
            for j in (i['children']):
                # print("\t",j['level'],j['label'],j['pnum'])
                cata.append([int(j['level']), j['label'], int(j['pnum'])])
    return cata

def pic2pdf(book_id):
    name, page_all = get_name(book_id)
    print("开始进行{}_{},一共{}页".format(book_id,name,page_all))
    #生成目录
    path_tem = path_raw+"\\temp\\{}_{}_P{}.pdf".format(name, book_id, page_all)
    path_final = path_raw+"\\{}_{}_P{}.pdf".format(name, book_id, page_all)
    imgList = os.listdir(path_raw)
    print("获取了{}页图片".format(len(imgList)))
    imgList.sort(key=lambda x:int(x[:-4]))
    img_all=[]
    for img in imgList:
        img_path=path_raw+"\\{}".format(img)
        img_all.append(img_path)
    pfn_bytes = img2pdf.convert(img_all, with_pdfrw=False);
    #判断临时目录是否存在
    if os.path.exists(path_raw+"\\temp") == False:
        os.mkdir(path_raw+"\\temp")
    with open(path_tem, "wb") as f:
        f.write(pfn_bytes)
        f.close()
    print("准备生成目录...")
    #添加目录
    try:
        doc = fitz.open(path_tem)
        toc = get_cata(book_id)
        doc.setToC(toc)
        doc.save(path_final)  # 保存pdf文件
        doc.close()
        print("保存成功{}".format(path_final))
        os.remove(path_tem)
        os.remove(path_raw+"\\temp")
    except:
        print("添加目录错误,检查页码是否正确")

#获取cookies
with open('Cookies.txt', 'r') as f:
    cookies=f.read()
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    'Cookie': cookies,
    'Host': 'lib-nuanxin.wqxuetang.com',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'none',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}
book_id=input('输入书的id:\n')
path_raw=input('输入放着图片的文件夹的全部路径:\n')
pic2pdf(book_id)

3.3 20200220更新,不需要cookies了,代码直接复制就能用
[Python] 纯文本查看 复制代码
import json
import os
import fitz
import img2pdf
import requests as req
def get_name(book_id):
    url_name = 'https://www.wqxuetang.com/v1/book/initbook?bid={}'.format(book_id)
    response = req.post(url=url_name, headers=headers)
    book_more=json.loads(response.text)
    return(book_more['data']['name'],book_more['data']['pages'])
def get_cata(book_id):
    cata=[]
    url_name='https://www.wqxuetang.com/v1/book/catatree?bid={}'.format(book_id)
    response = req.post(url=url_name, headers=headers)
    book_cata = json.loads(response.text)
    for i in book_cata['data']:
        cata.append([int(i['level']), i['label'], int(i['pnum'])])
        # print(i['level'],i['label'],i['pnum'])
        if ('children' in i):
            for j in (i['children']):
                # print("\t",j['level'],j['label'],j['pnum'])
                cata.append([int(j['level']), j['label'], int(j['pnum'])])
    return cata

headers = {
    'Accept': 'application/json,text/plain,*/*',
    'Accept - Encoding': 'gzip,deflate,br',
    'Accept - Language': 'zh-CN,zh;q = 0.9',
    'BA': 'bapkg/com.bookask.wqxuetang,baver/0.0.1',
    'Connection': 'keep - alive',
    'Cookie':'',
    'Host':'www.wqxuetang.com',
    'Referer':'https://www.wqxuetang.com/',
    'User - Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}
def pic2pdf(book_id):
    name, page_all = get_name(book_id)
    print("开始进行{}_{},一共{}页".format(book_id,name,page_all))
    #生成目录
    path_tem = path_raw+"\\temp\\{}_{}_P{}.pdf".format(name, book_id, page_all)
    path_final = path_raw+"\\{}_{}_P{}.pdf".format(name, book_id, page_all)
    imgList = os.listdir(path_raw)
    print("获取了{}页图片".format(len(imgList)))
    imgList.sort(key=lambda x:int(x[:-4]))
    img_all=[]
    for img in imgList:
        img_path=path_raw+"\\{}".format(img)
        img_all.append(img_path)
    pfn_bytes = img2pdf.convert(img_all, with_pdfrw=False);
    #判断临时目录是否存在
    if os.path.exists(path_raw+"\\temp") == False:
        os.mkdir(path_raw+"\\temp")
    with open(path_tem, "wb") as f:
        f.write(pfn_bytes)
        f.close()
    print("准备生成目录...")
    #添加目录
    try:
        doc = fitz.open(path_tem)
        toc = get_cata(book_id)
        doc.setToC(toc)
        doc.save(path_final)  # 保存pdf文件
        doc.close()
        print("保存成功{}".format(path_final))
        os.remove(path_tem)
    except:
        print("添加目录错误,检查页码是否正确")

flag=input("单独添加目录输入1\n")
if flag=='1':
    print("-进入单独添加目录-")
    try:
        book_id = input('输入书的id:\n')
        path = input("输入文件目录包括pdf名称")
        doc = fitz.open(path)
        toc = get_cata(book_id)
        doc.setToC(toc)
        doc.save(path)  # 保存pdf文件
        doc.close()
        print("添加成功{}".format(path))
    except:
        print("添加目录失败")


else:
    print("-进入PDF合成及目录添加-")
    book_id = input('输入书的id:\n')
    path_raw = input('输入放着图片的文件夹的全部路径:\n')
    pic2pdf(book_id)


4.参考文献
https://zhuanlan.zhihu.com/p/88618967
https://www.jb51.net/article/160622.htm

点个热
5.软件下载地址
https://www.52pojie.cn/thread-1103527-1-1.html

免费评分

参与人数 6吾爱币 +8 热心值 +5 收起 理由
ymhld + 1 + 1 我很赞同!
gxmer + 1 + 1 我很赞同!
::: + 1 我很赞同!
a1239761234 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
苏紫方璇 + 3 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
ligxi + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 858043016 发表于 2020-2-12 23:17
a1239761234 发表于 2020-2-12 22:55
大佬,cookie的Name叫什么?我好复制value 我不知道哪个是文泉的cookie,错误信息提示我没有登录

header里面的cookie全部复制
举个例子
acw_tc=276aedd815809635644848902e0e4fd9rcaad885f1e7ea46e726ce05e837d3; _gid=386761085674; _gidv=09dc965br4136047b48175910971a625; PHPSESSID=iquouitsjisgd32djh50nmgukv; Hm_lvt_a84b27ffd87daa3273555205ef60tdf89=1581489269,1581490795,1581491247,1581506791; ssxmod_itna=eqAOqjxGxfxmxCuDBpOt7DC0tp255GBCh1x0HoGzDAxn40iDt=cbmEQcg0AmxORq0QglmxP5WOeO4itqQf0eUrDU4i8DCdBAQTDeWQD5xGoDPxDeDADYojDAqiOD7d=DEDmRODYxGAuhxitD4qDBF23DKqGgtqhGWBQ/j4iBin6GBMGuD0tPxBdKleb4VQWPUum0DB=axBQgGMj9ieDHRhDTDO+ES3AC5D4PCGEQUEHvFYYIC+GoWUq5uGhPUA83WDDAKihLXiD==; ssxmod_itna2=eqAOqjxGxfxmxCuDBpOt7DC0tp2rGBChxnFoWI4Dsq3oDLBmpxsBGnSDw7uj9q0Q9R6uYdD6QDI47k/cCgDYL+4wxdL7bUQ8UIqomE3TztKnfLXatK+CynaeYDqO6eDOWsDGCj=K+zWRNlBLEyAdt/FS=Yjh3Y9rEMRx+q2GYG7QIxKwDZWf5l4SRe5qTpl4ASbm3Khw3o54wFfTn7OZr9fTXBAqEeugyzER6GpwH1pjy2meUC7Wn+btUqECPp9d3fzLt52M0OgzT743YjadUjrbCR9IQBDXCpgryhUFnyMhifnMU/1bInu6Fhzt9iGPRm4aAwfbHu3qzb5n+Ya8bT7D8CFIzaItW4H5YVaL0F=DFIWRRnafA9Ben2zuhYK9Ih6FOkAKsAgY6xxi5gWvkFYS9w5dSxCa1zKO7vU4ys7SQ6K3Onc8f1SoSR7r=vS3G=Q48SFWWGqAO1IPW8oTkyPphNXeQnobiiiFRQYGs44e4+OwGbZwqQdEWSjR2zafE7ww+ONdxDKwPD7=DYI4eD==; Hm_lpvt_a84b27ffd87daa3273555205ef60df89=1581520575
wtujcf123 发表于 2020-2-23 12:53
本帖最后由 wtujcf123 于 2020-2-23 12:54 编辑

你好,出现以下提示,请问如何解决
开始进行。。。
获取了951页图片
WARNING:root:Image contains transparency which cannot be retained in PDF.
WARNING:root:img2pdf will not perform a lossy operation.
WARNING:root:You can remove the alpha channel using imagemagick:
WARNING:root:  $ convert input.png -background white -alpha remove -alpha off output.png
Traceback (most recent call last):
  File "D:\桌面\11.py", line 89, in <module>
    pic2pdf(book_id)
  File "D:\桌面\11.py", line 49, in pic2pdf
    pfn_bytes = img2pdf.convert(img_all, with_pdfrw=False);
  File "C:\Users\wtu\AppData\Local\Programs\Python\Python36\lib\site-packages\img2pdf.py", line 1829, in convert
    ) in read_images(rawdata, kwargs["colorspace"], kwargs["first_frame_only"]):
  File "C:\Users\wtu\AppData\Local\Programs\Python\Python36\lib\site-packages\img2pdf.py", line 1225, in read_images
    imgdata, imgformat, default_dpi, colorspace, rawdata
  File "C:\Users\wtu\AppData\Local\Programs\Python\Python36\lib\site-packages\img2pdf.py", line 990, in get_imgmetadata
    raise Exception("Refusing to work on images with alpha channel")
Exception: Refusing to work on images with alpha channel
miqi1314 发表于 2020-2-12 19:00
圣皇 发表于 2020-2-12 19:18
感谢分享
I5U5P 发表于 2020-2-12 19:54
谢谢分享
威士忌 发表于 2020-2-12 20:47
怎么下载  是个问题  最新好像不行了
wyzjezm 发表于 2020-2-12 21:19
楼主能否编译成exe文件方便我们这些小白直接使用。
aniu 发表于 2020-2-12 21:31
有下载的pythOn吗?
ymhld 发表于 2020-2-12 22:03
大佬这个好像合成JPG和加目录的吧
 楼主| 858043016 发表于 2020-2-12 22:12
ymhld 发表于 2020-2-12 22:03
大佬这个好像合成JPG和加目录的吧

是的,我又进行了修改,这个生成pdf把我12G内存跑满了都
ymhld 发表于 2020-2-12 22:12
858043016 发表于 2020-2-12 22:12
是的,我又进行了修改,这个生成pdf把我12G内存跑满了都

那估计是合成PDF时弄的,没合成对
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 16:11

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表