代码拉取完成,页面将自动刷新
from bs4 import BeautifulSoup
import requests
from tqdm import trange
import urllib
import shutil
import os
from img2pdf import conpdf
import requests
def download(url, callback):
try:
text = requests.get(url).text
pos = text.index('allPage:')
pages = int(text[pos + 8: pos + 12].split(',')[0])
id = url.split('.')[-2].split('-')[-1]
html = BeautifulSoup(text, features='lxml')
title = html.title.string.replace('/', '.')
print(f'豆丁:《{title}》')
if os.path.exists(f'./output/{title}.pdf'):
return True, title
if os.path.exists(f'./temp/{title}'):
shutil.rmtree(f'./temp/{title}')
os.makedirs(f'./temp/{title}')
for i in trange(pages):
url = f"http://211.147.220.164/index.jsp?file={id}&width=1600&pageno={i + 1}"
res = requests.get(url)
with open(f'./temp/{title}/{i+1}.jpg', 'wb') as f:
f.write(res.content)
callback(i, pages, "正在下载:%s" % title)
print('下载完毕,正在转码')
callback(99, 100, "正在转码")
conpdf(f'output/{title}.pdf', f'./temp/{title}', '.jpg', True)
return True, title
except Exception as e:
return False, e
if __name__ == "__main__":
download("https://jz.docin.com/p-1995868152.html")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。