首页 > 编程知识 正文

python向excel追加数据,怎样将python代码保存成文件

时间:2023-05-06 10:09:18 阅读:146537 作者:2670

注意事项:

1、模块xlwt适合写入xls格式的excel

2、模块openpyxl适合xlsx格式的excel写入

3、使用xlwt写入xlsx格式的excel时,excel可能无法打开

4、使用xlwt以0行开始,使用openpyxl以1行开始

importrequestsfrombs4importbeautifulsoupimportxlwtdefrequest _ douban (URL ) : try : headers={ ' accept ' 3360 ' t exchaders q=0.8,APP /信号交换; v=b3; q=0.9 ',' Accept - Encoding': 'gzip,deflate,br ',' Accept-Language': 'zh-CN,zh; q=0.9 ',' Connection': 'Keep-Alive ',' Host': 'movie.douban.com ',' user-agent ' 3360 ' Mozilla wwa x64 ) appleWebKit/537.36(khtml, like Gecko ) chrome/91.0.4472.101 safari/537.36 ' } response=requests.get ) ) UUUE timeout=3) if response.status _ code==200: return response.textexceptrequests . find(class_='grid_view ' ).find_all('Li ' ) book=xlwt.workbook ) encoding=' utf-8 syle _ compresiok eet.write )、' 0、' 0、“排名”(sheet.write ) 0、3、“评分”(sheet.write ) 0、4、“作者”(sheet.write ) 0、5 '个人资料' (I=1foriteminlist : item _ name=item.find (class _=' title ' ).stringitem_img=item.find ) ' ' _score=item.find ) class_='rating_num ' ).stringitem ) )。 text item _ intr=item.find (class _=' inq ' ).stringsheet.write(I,0,item_name ) shet.write ) I,1,1 item_intr ) i=1 book.save(u ) u '豆瓣最大的start='str(page*25 ) filter='lxml=request_douban ) URL ) souban

importopenpyxlasopenpyxlimportrequestsfrombs4importbeautifulsoupfromopenpyxlimportworkbookfromopenpyxl.utilsimportget _ coll : headers={ ' accept ' : ' text/html,application/xhtml xml,application/xml; q=0.9,image/avif,image/webp,image/apng,*/*; q=0.8,APP /信号交换; v=b3; q=0.9 ',' Accept - Encoding': 'gzip,deflate,br ',' Accept-Language': 'zh-CN,zh; q=0.9 ',' Connection': 'Keep-Alive ',' Host': 'movie.douban.com ',' user-agent ' 3360 ' Mozilla wwa x64 ) appleWebKit/537.36(khtml, like Gecko ) chrome/91.0.4472.101 safari/537.36 ' } response=requests.get ) ) UUUE timeout=3) if response.status _ code==200: return response.textexceptrequests . find(class_='grid_view ').find_all('Li ' ) book=Workbook ) sheet=book.activesheeet value='名称' ) column=3,value='排名') shalue column=5,value='作者') sheet.cell(row=1,column=6,value='简介' (前言) item_index=item.find(class_=' ' ).stringitem_score=item.find ) class_='rating_num ' ).string itittid . text item _ intr=item.find (class _=' inq ' ).stringsheet.cell(row=I,column=1,value=item_name ) ) value 652 value=item _ img (sheet.cell (row=I,column=3,value=item_index ) sheet.cell ) row=I,column=value=item_intr ) i=1 book.save(u ) u '豆瓣最受欢迎的250部电影. xlssave start='str(page*25 ) filter=' lxml=reqml

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。