1.xlutils、xlrd用来修改excel
import xlrd from xlutils import copy book = xlrd.open_workbook("test.xls") new_book = copy.copy(book) sheet = new_book.get_sheet(0) sheet.write(0,0,"编号") sheet.write(0,1,"名字") new_book.save("test.xls")
2.openpyxl 是用于读取和写入 Excel 2010 xlsx / xlsm / xltx / xltm 文件的 Python 库。
import openpyxl book = openpyxl.load_workbook("test2.xlsx") #load_workbook()方法打开文件。 sheet = book["Sheet1"] print(book.sheetnames) #所有sheet页的名字 print(sheet.max_row) #多少行 print(sheet.max_column) #多少列 sheet.cell(1,1).value = "id" #修改某个元素的值 print(dir(sheet)) print(sheet["A1"].value) print(sheet["A2"].value) sheet["A1"] = "编号" #修改字段名 book.save("test2.xlsx") #保存文件
3.schedule用来进行定时任务的管理
import schedule import time def test(): print("I'm working...") def test2(): print("I'm working... in job2") # 每10分钟执行一次job函数 schedule.every(10).minutes.do(test) # 每10秒执行一次job函数 schedule.every(10).seconds.do(test) # 当every()没参数时默认是1小时/分钟/秒执行一次job函数 schedule.every().hour.do(test) schedule.every().day.at("10:30").do(test) schedule.every().monday.do(test) # 具体某一天某个时刻执行一次job函数 schedule.every().wednesday.at("13:15").do(test) # 可以同时定时执行多个任务,但是每个任务是按顺序执行 schedule.every(10).seconds.do(job2) # 如果job函数有有参数时,这么写 schedule.every(10).seconds.do(job,"参数") while True: # 启动服务 schedule.run_pending() time.sleep(1)
4.yamail用来发送邮件
import yamail username = "input username " password = "input password " host = "smtp.qq.com" #qq # host = "smtp.163.com" #163 # host = "smtp.126.com" #qq smtp = yamail.SMTP(host=host,user=username,password=password) # smtp.send(to="this is email") smtp.send( to=["this is email","this is email"], cc=["this is email","this is email"], subject="课后好好学习", contents="下课之后先吃饭,吃完饭明天好好学习。", attachments=["a.jpg","a.xls"] )
5.urllib处理请求
from urllib.request import urlopen from urllib.parse import urlencode,urljoin,quote,quote_plus,unquote,unquote_plus # host = "http://api.nnzhp.cn/?stu_name=小黑" # print(quote_plus(host)) #url编码 # print(quote(host)) url = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=2&tn=baiduhome_pg&wd=%E4%BD%A0%E5%A5%BD%E6%96%BD%E5%B7%A5%E9%98%9F&rsv_spt=1&oq=sdgsdsdg&rsv_pq=cbca1b490021d566&rsv_t=83a3dvr2MgkSUHYndjTkUaly8mj0O2gEaU6FiQmNXisrrzrJ0W3nU6KvAPnPtdK%2FyfiU&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_sug3=23&rsv_sug1=10&rsv_sug7=101&rsv_sug2=0&rsv_btype=t&inputT=4151&rsv_sug4=4151" # url = urljoin(host,'/api/login') print(unquote(url)) print(unquote_plus(url)) # url = "http://api.nnzhp.cn/api/user/stu_info" data = {"stu_name":"xiaohei","age":18} # print(urlencode(data)) # # req = urlopen(url,urlencode(data).encode() ) #post请求 # req = urlopen(url+'?'+urlencode(data)) # print(req.read().decode())
6.requests处理网络请求
import requests #-------------------get请求---------------- # url = "http://api.nnzhp.cn/api/user/stu_info" # data = {"stu_name":"小"} # req = requests.get(url,data) #-------------------post请求---------------- # url = "http://api.nnzhp.cn/api/user/login" # data = {"username":"niuhanyang","passwd":"aA123456"} # data2 = {"version":1} # req = requests.post(url,params=data2,data=data) # print(req.url) #查看请求发出去的url #http://api.nnzhp.cn/api/user/login?version=1 #params参数是在url里面的 #data参数是在body里面的 # url = "http://api.nnzhp.cn/api/user/add_stu" # data = { # "grade": "飞马座", # "phone": "22345678992", # "name":"哈哈哈哈" # } # req = requests.post(url,json=data) # url = "https://qun.qq.com/cgi-bin/qun_mgr/search_group_members" # data = { # "gc": '' , # "st": "21", # "end": "41", # "sort": 0, # "bkn": "" # } # cookie = {'':''} #'cookie中值写成key:value形式 #req = requests.post(url,data,cookies=cookie) url = "https://qun.qq.com/cgi-bin/qun_mgr/search_group_members" data = { "gc": 180208520, "st": "21", "end": "41", "sort": 0, "bkn": "1391997515" } header = { "cookie":"RK=XYYhgDpPfy; ptcz=a5b23b4f93bc2f304119957286b77b21be95814358d482ef0a2bf216733e57ba; pgv_pvid=5931324545; uin=o0511402865; skey=@HHempil2m; p_uin=o0511402865; pt4_token=tkeLBl-znMjpBJxIv1Chj*kLx7p0dsh4BkZfF39cpDk_; p_skey=OS7gS9Zw8*r*aIpqd5-S5BUo65EJMNWRkLLMO1Vpu5o_; traceid=5b35fe5632", "user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36" } req = requests.post(url,data,headers=header) result=req.json() print(result) # url = "http://api.nnzhp.cn/api/file/file_upload" # data = {"file":open("a.xls",'rb')} # req = requests.post(url,files=data) # print(req.json()) #返回字典,如果接口返回的不是json,那会报错 # url = "http://aliimg.changba.com/cache/photo/941190975_200_200.jpg" # # req = requests.get(url,verify=False) # # with open("a.jpg",'wb') as fw: # fw.write(req.content) # print(req.json()) # print(req.text) #返回的是字符串 # print(req.content) #返回的是bytes的,包括图片,媒体等等 # print(req.status_code) #返回的状态码 # print(req.cookies) #返回的cookie # print(req.headers) #返回的header