15、解决14中csv用excel打开乱码的问题 open('zhihu.csv','w',newline='',encoding='utf-8-sig')...
生活随笔
收集整理的這篇文章主要介紹了
15、解决14中csv用excel打开乱码的问题 open('zhihu.csv','w',newline='',encoding='utf-8-sig')...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
解決14中csv用excel打開亂碼的問題 ,其實就是在寫csv的時候把 utf-8 改成 utf-8-sig
open('zhihu.csv','w',newline='',encoding='utf-8-sig')
這個和csv文件的bom有關,參考下面信息
?
?
下面是在練習14的基礎上改一下編碼,效果見下面截圖
?
1 # 爬取知乎大v張佳瑋的文章“標題”、“摘要”、“鏈接”,并存儲到本地文件 2 # URL https://www.zhihu.com/people/zhang-jia-wei/posts 3 4 import requests 5 import time 6 import openpyxl 7 import csv 8 9 headers = { 10 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', 11 'accept-language': 'zh-CN,zh;q=0.9', 12 'cache-control': 'max-age=0', 13 'cookie':'__DAYU_PP=iJb63REJnnjIMmBvzNMV65ab0a6aae4f; q_c1=d75d908a13c44b95bd75f27578ad2088|1521641428000|1521641428000; _zap=bec28151-809b-4936-971a-d18f5255add0; tgw_l7_route=f2979fdd289e2265b2f12e4f4a478330; _xsrf=wQDRNSLBlRv3aimMzhUNyqg1BpLUnWAr; d_c0="ABDmoGi2RQ-PTp5SSFyQvDgz_QEjeQfCFgk=|1555156366"; capsion_ticket="2|1:0|10:1555156366|14:capsion_ticket|44:MTBhN2FkYjYyNWEyNDFjYWJiYTk2N2E1YTA1NDE4OTk=|084e15694c6993269b3aab564e9ea5d7983782f4b37dbc4537e000aa7b081901"', 14 'upgrade-insecure-requests': '1', 15 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36' 16 } 17 18 res = requests.get('https://www.zhihu.com/api/v4/members/zhang-jia-wei/articles?include=data%5B*%5D.comment_count%2Csuggest_edit%2Cis_normal%2Cthumbnail_extra_info%2Cthumbnail%2Ccan_comment%2Ccomment_permission%2Cadmin_closed_comment%2Ccontent%2Cvoteup_count%2Ccreated%2Cupdated%2Cupvoted_followees%2Cvoting%2Creview_info%2Cis_labeled%2Clabel_info%3Bdata%5B*%5D.author.badge%5B%3F(type%3Dbest_answerer)%5D.topics&offset=0&limit=20&sort_by=created',headers=headers) 19 20 totals = res.json()['paging']['totals'] 21 num = 0 22 23 # excel 表頭部分 24 25 wb = openpyxl.Workbook() 26 sheet = wb.active 27 sheet.title = '張佳瑋的文章' 28 sheet['A1'] = '編號' 29 sheet['B1'] = '標題' 30 sheet['C1'] = '創建時間' 31 sheet['D1'] = '鏈接' 32 sheet['E1'] = '摘要' 33 34 # csv 表頭部分 35 36 with open('zhihu.csv','w',newline='',encoding='utf-8-sig') as csv_file: 37 writer = csv.writer(csv_file) 38 writer.writerow(['編號','標題','創建時間','鏈接','摘要']) 39 40 for offset in range(0,21,20): 41 res = requests.get('https://www.zhihu.com/api/v4/members/zhang-jia-wei/articles?include=data%5B*%5D.comment_count%2Csuggest_edit%2Cis_normal%2Cthumbnail_extra_info%2Cthumbnail%2Ccan_comment%2Ccomment_permission%2Cadmin_closed_comment%2Ccontent%2Cvoteup_count%2Ccreated%2Cupdated%2Cupvoted_followees%2Cvoting%2Creview_info%2Cis_labeled%2Clabel_info%3Bdata%5B*%5D.author.badge%5B%3F(type%3Dbest_answerer)%5D.topics&offset={}&limit=20&sort_by=created'.format(offset),headers=headers) 42 html = res.json() 43 items = res.json()['data'] 44 for item in items: 45 num = num + 1 46 print(num,end='\t') 47 timeArray = time.localtime(item['created']) 48 print(time.strftime("%Y-%m-%d %H:%M:%S",timeArray),end='\t') 49 print(item['title']) 50 51 # excel 內容部分 52 sheet.append([num,item['title'],time.strftime("%Y-%m-%d %H:%M:%S",timeArray),item['url'],item['excerpt']]) 53 54 # csv 內容部分 55 writer.writerow([num,item['title'],time.strftime("%Y-%m-%d %H:%M:%S",timeArray),item['url'],item['excerpt']]) 56 57 # excel 保存到文件 58 wb.save('zhihu.xlsx')?
轉載于:https://www.cnblogs.com/www1707/p/10720594.html
總結
以上是生活随笔為你收集整理的15、解决14中csv用excel打开乱码的问题 open('zhihu.csv','w',newline='',encoding='utf-8-sig')...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网站大访问量优化整体思路
- 下一篇: JAVA 多用户商城系统b2b2c---