1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| import requests import json import pandas as pd
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Host': 'portal.jictrust.cn', 'Pragma': 'no-cache', }
def getCookies(cookie_str): cookie_items = cookie_str.split(';') cookie_dict = {} for item in cookie_items: key, value = item.strip().split('=', 1) cookie_dict[key] = value return cookie_dict
def getDownloadPath(cookies,params): url = r'http://xxx/api/onekeysave/save' result = requests.post(url=url,headers=headers,cookies=cookies,json=params) return result.text
def getFiles(cookies,downloadPath) : response = requests.get(downloadPath, headers=headers, cookies=cookies) return(response)
def downloadFiles(response,filetype,filename): path = r'D:\workspace\新公文系统\2022年\{}\{}'.format(filetype,filename) with open(path ,'wb') as f: f.write(response.content) print('文件下载完成')
if __name__ == '__main__': df = pd.read_excel(r'C:\新公文系统.xlsx',sheet_name='Sheet1',engine='openpyxl') cookie_str = r'ecology_JSessionid=aaaxxx8Ok_L7Vy; JSESSIONID=aaaLxxxOk_L7Vy; loginxxxaver=xxx; languxxxaver=xx; __randcode__=c9165f7d-9xxxxxxdf62bb4b' cookie_dict = getCookies(cookie_str) for index,row in df.iterrows(): print(index,'_',row[1],'_下载开始:') fwzh = '' if pd.isnull(row[1]): fwzh = "" else: fwzh = row[1] params = {"fwzh":fwzh,"fwtm":row[2],"requestid":str(row[3]),"maintable":"formtable_main_233","zw":"zw","fj":"fj","path":"D:/weaver/ecology/page/resource/userfile/other/","configip_target":"80","OA_APPID":"b81a21daxxxf2d5bd","category":"1"} download_path = getDownloadPath(cookie_dict,params) download_path = json.loads(download_path) downloadUrl = 'http://xxx' + download_path['data']['loadlink'] response = getFiles(cookie_dict,downloadUrl) downloadFiles(response,row[0],str(row[3])+'_'+download_path['data']['filename']) print('--------------------')
|