Python常用脚本-查询数据库,调用网站接口,下载文件保存到本地

# coding=utf-8
# 证照模板下载,查询中心库,调用网站接口,下载文件保存到本地
import requests
import json
import datetime
import xlwt
import os
import cx_Oracle as oracle


def download(url,fname):
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36',
        'Referer':'http://59.*03.1*3.182:8800/zzwk/cat**og/certificateS***leView.do?id=ff80808175b737d50175e37151e6171a',
        'Host':'59.203.153.1**:8800',
        'method':'GET',
        'Origin':'http://59.**3.1*3.182:8800',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Cookie':'JSESSIONID=480B303CC5E1BAD338442CAEBF3C1659.tomcat8080; loginType=normal; cookie=20111181'
    }
    response = requests.get(url=url, headers=headers)
    filename =fname+".zip"
    print(filename)
    with open('d:\\dc\\'+filename, "wb") as code:
        code.write(response.content)


# 查询数据库,获取结果集
def get_site_id_by_name():
    db = oracle.connect('cent**/123**6@17*.16.10.**7:31521/center')
    cursor = db.cursor()
    qsite_sql = "SELECT * FROM TYQ_ZZMLXZ"
    cursor.execute(qsite_sql)
    data = cursor.fetchall()
    print(data)
    return data


if __name__ == '__main__':
   zzdata=get_site_id_by_name()
   for i in zzdata:
            id=i[2]
            name=i[1]
            url='http://59.2**.153.1**:8800/zzwk/cat**og/downloadModelById.do?modelId='+id;
            # 下载
            download(url,name);

  

posted @ 2021-08-20 09:35  伍叶春  阅读(354)  评论(0编辑  收藏  举报