Python3 将百词斩已经学过单词爬取保存到excel

来源:互联网 发布:ubuntu终端命令大全 编辑:程序博客网 时间:2024/04/30 08:32

没怎么学过Python,只是突然有个需求,百词斩无法将已经学过的单词导出,很是郁闷。遂安装了Python,通过搜索,整合了一个能够导出到excel的代码

主要学习了博主的代码
http://blog.csdn.net/greenapple_shan/article/details/50492536

可惜没有导出功能,于是又加入了excel

引用库xlwt,地址https://pypi.python.org/pypi/xlwt/#downloads。测试也可以用pip install xlwt直接安装

最终代码

import urllibimport urllib.requestimport http.cookiejarimport jsonimport xlwtemail = '邮箱或者手机号'pwd = '密码'data = {'email':email,'raw_pwd': pwd}post_data = urllib.parse.urlencode(data).encode(encoding='UTF8')opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()))response = opener.open('http://www.baicizhan.com/login', post_data)# # print(response .read())wb = xlwt.Workbook()ws = wb.add_sheet('A Test Sheet')row=0for i in range(1, 60):    content = json.loads(opener.open("http://www.baicizhan.com/user/all_done_words_list?page=%s"%i).read())    for word in content["list"]:        ws.write(row, 0, word["wrong_times"])        ws.write(row, 1, word["word"])        ws.write(row, 2, word["word_meaning"].strip())        row+=1wb.save('example.xls')

小问题

从上个博主copy过来的代码应该是Python2的,用在Python3上会报错。主要修改

urllib2 -> urllib.request
cookielib -> http.cookiejar
urllib.urlencode(values)-> urllib.parse.urlencode(values).encode(encoding=’UTF8’)

原创粉丝点击