Loading... 1)写入图片 ```python #传入图片地址,文件名,保存单张图片 def saveImg(self,imageURL,fileName): u = urllib.urlopen(imageURL) data = u.read() f = open(fileName, 'wb') f.write(data) f.close() ``` ```python 2)写入文本 def saveBrief(self,content,name): fileName = name + "/" + name + ".txt" f = open(fileName,"w+") print u"正在偷偷保存她的个人信息为",fileName f.write(content.encode('utf-8')) ``` 3)创建新目录 ```python #创建新目录 def mkdir(self,path): path = path.strip() # 判断路径是否存在 # 存在 True # 不存在 False isExists=os.path.exists(path) # 判断结果 if not isExists: # 如果不存在则创建目录 # 创建目录操作函数 os.makedirs(path) return True else: # 如果目录存在则不创建,并提示目录已存在 return False ``` 4) 使用openpyxl存入excal ```python # #!user/bin/env python3 # -*- codeing:utf-8 -*- # Email 2319899766@QQ.com # Time : 2021/4/24 23:15 from openpyxl import Workbook # 能写接口就写接口 def save_page_excal(all_list): # 打开excal # 创建excal,填写表头 wb = Workbook() # 激活工作表 ws = wb.active # 设置表头 ws.append(['工作名称', '工作地区', '发布时间', '发布公司', '工资', '经验', '类别', '规模', '技术','优势']) for list in all_list: job_name=list[0] job_region=list[1] job_format_time=list[2] job_company=list[3] job_wages=list[4] job_experience=list[5] job_categor=list[6] job_scale=list[7] job_technology=list[8] job_advantage=list[9] # 定义出入的参数 line=[job_name,job_region,job_format_time,job_company,job_wages,job_experience,job_categor,job_scale,job_technology,job_advantage] ws.append(line) wb.save('Job.xlsx') all_list=[ ['a','b','c','b','c','b','c','b','c','b'],['a','b','c','b','c','b','c','b','c','b'],['a','b','c','b','c','b','c','b','c','b'],['a','b','c','b','c','b','c','b','c','b'] ] save_page_excal(all_list) ``` 5)存入mysql ```python #!user/bin/env python3 # -*- codeing:utf-8 -*- # Email 2319899766@QQ.com # Time : 2021/4/24 23:18 import pymysql def save_page_mysql(all_list): # 存入mysql 这里注意,链接mysql和关闭mysql都是只有一次操作的,所以都写到另外的def里 # 连接MySQL数据库 connect = pymysql.connect(host='localhost', user='root', password='123456', db='lagou', port=3306) cursor = connect.cursor() for list in all_list: job_name = list[0] job_region = list[1] job_format_time = list[2] job_company = list[3] job_wages = list[4] job_experience = list[5] job_categor = list[6] job_scale = list[7] job_technology = list[8] job_advantage = list[9] try: cursor.execute( 'insert into job(job_name,job_region,job_format_time,job_company,job_wages,job_experience,job_categor,job_scale,job_technology,job_advantage) VALUES ("{}","{}","{}","{}","{}","{}","{}","{}","{}","{}")'.format( job_name,job_region,job_format_time,job_company,job_wages,job_experience,job_categor,job_scale,job_technology,job_advantage)) connect.commit() except: print('获取失败' + job_name) cursor.close() connect.close() all_list=[ ['a','b','c','b','c','b','c','b','c','b'],['a','b','c','b','c','b','c','b','c','b'],['a','b','c','b','c','b','c','b','c','b'],['a','b','c','b','c','b','c','b','c','b'] ] save_page_mysql(all_list) ``` 最后修改:2022 年 12 月 02 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏