各位老哥早上好,家里孩子上初中了经常需要各种试卷,我负责给他找试卷,主要用到的几个网站:学科网、组卷网和菁优网,之前有个 python 脚本可以把学科网的预览页面进行解析生成 html 文档,然后借助浏览器打印成 pdf 。但是最近学科网的文档预览机制(或者代码)好像做了修改,现在能够获取到的内容只有一部分,之前是可以全部预览的。那位老哥路过有时间帮忙看看或者分析下。 之前使用的代码如下:
import requests
from re import findall, sub
from hashlib import md5
HTML_FORMAT ='''
<html>
<head>
<title>{title}</title>
</head>
<body>
{body}
</body>
</html>
'''
def writefile(filename,text):
filename = sub(r"""[\*\/\\\|\<\>\? \:\.\'\"\!]""", "", filename)
unique = md5(text.encode())
filename += "_"+unique.hexdigest()[:5]
filename+=".html"
print("Writing "+filename)
# print("-=-=-=-=\n",text,"\n-=-=-=-=")
with open(filename+'.html', 'w', encoding="utf-8") as f:
f.write(text)
def main():
softID=input("ID: ")
url = "https://www.zxxk.com/soft/Preview/FirstLoadPreviewJson?softID={}&type=3&product=1&v=2&FullPreview=true"
response = requests.get(url.format(softID))
if response.status_code!=200:
print("ERROR")
print(response.status_code)
return -1
ret=response.json()["data"]
if not ret["IsSuccess"]:
print("ERROR: IsSuccess option is not true")
print(ret)
if not ret['IsRar']:
print("Not rar")
print("TotalPage=%d" % ret['TotalPage'])
print("SoftExt=%s" % ret['SoftExt'])
try:
html=ret["Html"]
print(ret)
except:
print(ret)
exit(1)
# replace "data-original" to "src" for showing in browser
html=html.replace("data-original", "src")
writefile(f"{softID}",html)
else:
print("is RAR")
rar=ret['rarPreviewInfo']
for file in rar:
html=file["Html"]
title=file["SoftName"]
# replace "data-original" to "src" for showing in browser
# html=html.replace("data-original", "src")
urls=findall("(?<=data-original=\") https://preview.zxxk.com/\\S+(?=\")",html)
l=[]
for url in urls:
if "jpg" in url:
l.append(f"<img src={url} />")
continue
page=requests.get(url,cookies=response.cookies)
if not page.status_code==200:
print(page)
print(page.status_code)
print(page.text)
assert page.status_code==200
l.append(page.text)
format_html=HTML_FORMAT.format(title=title,body="\n".join(l))
writefile(title,format_html)
if __name__ == "__main__":
while True:
main()
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.