|
[ol]import requestsimport redef get_filename_from_cd(cd): """ Get filename from content-disposition """ if not cd: return None fname = re.findall('filename=(.+)', cd) if len(fname) == 0: return None return fname[0]cookies = { 'JSESSIONID': 'fa496ef8-4c46-49ba-b74c-dfc4c66a1970',}headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', 'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1',}session = requests.Session()index = session.get("http://read.nlc.cn/OutOpenBook/OpenObjectBook?aid=403&bid=17912.0", headers=headers, cookies=cookies).texttoken = re.findall(r"tokenKey="(.*)" timeKey="(.*)" timeFlag", index)myreader = token[0][0]ktime = token[0][1]headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0', 'Accept': '*/*', 'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2', 'Accept-Encoding': 'gzip, deflate', 'myreader': myreader, 'Range': 'bytes=0-1', 'Connection': 'keep-alive', 'Referer': 'http://read.nlc.cn/static/webpdf/lib/WebPDFJRWorker.js?h=0.29527337592322755',}pdf = session.get("http://read.nlc.cn/menhu/OutOpenBook/getReader?aid=403&bid=17912.0&kime=" + ktime + "&fime=" + ktime, allow_redirects=True, headers=headers, cookies=cookies)filename = get_filename_from_cd(pdf.headers.get('content-disposition'))open(filename, 'wb').write(pdf.content)[/ol]复制代码 |
|