要求
实现功能:利用扇贝网:https://www.shanbay.com/, 做个测单词的小工具。
扇贝网已经有一个测单词量的功能,我们要做的就是把这个功能复制下来,并且做点改良,搞一个网页版没有的功能 ———— 自动生成错词本。
1.根据选择的题库,获取50个单词
点开检查->Network->选中Perserve log->查看XHR->点开刷新之后出现的XHR->查看Response
会发现题库在response这里,于是我们需要找到真正的URL,也就是在Headers的General里,找到requests URL,这里的URL为:
https://www.shanbay.com/api/v1/vocabtest/category/?_=1566620246391
打开这个URL的页面如下
但看不懂URL?后面是什么东西,我们试着把它去掉输入
https://www.shanbay.com/api/v1/vocabtest/category/
发现打开的页面和刚刚的一样
那么如何解析这种URL呢?
这就用到我们之前学过的json把response对象转换为字典/列表的方法。然后用input的方式让用户输入要选择的词库。这一部分的代码就是
import requests
res=requests.get('https://www.shanbay.com/api/v1/vocabtest/category/')
res_dict=res.json()
print(res_dict)
tiku_num=int(input('''请输入你想选择的题库:(输入数字即可)
1,GMAT 2,考研 3,高考 4,四级 5,六级 6,英专 7,托福 8,GRE 9,雅思 10,任意
'''))
print(res_dict['data'][tiku_num-1][1])
继续往下思考,现在假设用户选择的是六级,我们点击六级,查看之后的页面,点开最新加载出来的URL,发现这时的requests URL已经发生改变,点开response,发现我们要找的单词就在这个URL里。
接下来的步骤就很熟悉了,用requests.get()获取网页,然后用json解析并提取单词
import requests
res=requests.get('https://www.shanbay.com/api/v1/vocabtest/category/')
res_dict=res.json()
#print(res_dict)
tiku_num=int(input('''请输入你想选择的题库:(输入数字即可)
1,GMAT 2,考研 3,高考 4,四级 5,六级 6,英专 7,托福 8,GRE 9,雅思 10,任意
'''))
tiku_choice=res_dict['data'][tiku_num-1][1]
res=requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category={}'.format(tiku_choice))
res_dict=res.json()
#print(res_dict)
words_data=res_dict['data']
print(words_data)
得到的words_data是一个包含50个单词列表。
2.让用户选择认识的单词:此处要分别记录下用户认识哪些,不认识哪些。
import requests
res=requests.get('https://www.shanbay.com/api/v1/vocabtest/category/')
res_dict=res.json()
#print(res_dict)
tiku_num=int(input('''请输入你想选择的题库:(输入数字即可)
1,GMAT 2,考研 3,高考 4,四级 5,六级 6,英专 7,托福 8,GRE 9,雅思 10,任意
'''))
tiku_choice=res_dict['data'][tiku_num-1][1]
res=requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category={}'.format(tiku_choice))
res_dict=res.json()
#print(res_dict)
words_data=res_dict['data']
#print(words_data)
words_know=[]
words_notknow=[]
for data in words_data:
word=data['content']
word_know=input('认识{}吗?(认识的输入y,不认识请直接按Enter):'.format(word))
if word_know=='y':
words_know.append(word)
else:
words_notknow.append(word)
#print(words_know)
#print(words_notknow)
3.对于用户认识的单词,给选择题让用户做:此处要记录用户做对了哪些,做错了哪些。
import requests
res=requests.get('https://www.shanbay.com/api/v1/vocabtest/category/')
res_dict=res.json()
#print(res_dict)
tiku_num=int(input('''请输入你想选择的题库:(输入数字即可)
1,GMAT 2,考研 3,高考 4,四级 5,六级 6,英专 7,托福 8,GRE 9,雅思 10,任意
'''))
tiku_choice=res_dict['data'][tiku_num-1][1]
res=requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category={}'.format(tiku_choice))
res_dict=res.json()
#print(res_dict)
words_data=res_dict['data']
#print(words_data)
datas_know=[]
words_know=[]
words_notknow=[]
num=0
for data in words_data:
num+=1
word=data['content']
word_know=input('{}.认识{}吗?(认识的输入y,不认识请直接按Enter):'.format(num,word))
if word_know=='y':
words_know.append(word)
datas_know.append(data)
else:
words_notknow.append(word)
#print(datas_know)[{'content': 'government', 'pk': 2960, 'definition_choices': [{'pk': 3544, 'rank': 337, 'definition': ' v. 参加, 结合, 联合, 加入'}, {'pk': 1984, 'rank': 475, 'definition': ' n. 交易,协定,份量'}, {'pk': 2960, 'rank': 112, 'definition': ' n. 政府,政体,统治'}, {'pk': 5165, 'rank': 107, 'definition': ' adj. 正确的,正直的,合适的,垂直的,右面的,正常的,正面的'}], 'rank': 112},{'content': .....}]
print('现在检测你是否真的认识这些单词。')
right=[]
wrong=[]
for data in datas_know:
word=data['content']
print('A:'+data['definition_choices'][0]['definition'])
print('B:'+data['definition_choices'][1]['definition'])
print('C:'+data['definition_choices'][2]['definition'])
print('D:'+data['definition_choices'][3]['definition'])
choice=input('{}的意思是:'.format(word))
dict={'A':data['definition_choices'][0]['rank'],'B':data['definition_choices'][1]['rank'],'C':data['definition_choices'][2]['rank'],'D':data['definition_choices'][3]['rank']}
if dict[choice]==data['rank']:
right.append(word)
else:
wrong.append(word)
#print(right)
#print(wrong)
f=open('错题集.txt','a+',encoding='utf-8-sig')
f.write('你记错的单词有:\n')
for word in wrong:
#print(word,end='\t')
f.write(word)
f.write('\t')
f.write('\n你不认识的单词有:\n')
for word in words_notknow:
#print(word,end='\t')
f.write(word)
f.write('\t')
f.close()
4.生成报告:50个单词,不认识多少,认识多少,掌握多少,错了多少
import requests
res=requests.get('https://www.shanbay.com/api/v1/vocabtest/category/')
res_dict=res.json()
#print(res_dict)#{'msg': 'SUCCESS', 'status_code': 0, 'data': [['GMAT', 'GMAT'], ['NGEE', '考研'], ['NCEE', '高考'], ['CET4', '四级'], ['CET6', '六级'], ['TEM', '英专'], ['TOEFL', '托福'], ['GRE', 'GRE'], ['IELTS', '雅思'], ['NONE', '任意']]}
tiku_num=int(input('''请输入你想选择的题库:(输入数字即可)
1,GMAT 2,考研 3,高考 4,四级 5,六级 6,英专 7,托福 8,GRE 9,雅思 10,任意
'''))
tiku_choice=res_dict['data'][tiku_num-1][1]
res=requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category={}'.format(tiku_choice))
res_dict=res.json()
#print(res_dict)#{'msg': 'SUCCESS', 'status_code': 0, 'data': [{'content': 'information', 'pk': 3435, 'definition_choices': [{'pk': 311, 'rank': 43, 'definition': ' n. 许多人,许多'}, {'pk': 4611, 'rank': 219, 'definition': ' n. 人'}, {'pk': 4542, 'rank': 222, 'definition': ' n.社交聚会; 党,党派; 当事人; 同类,伙伴'}, {'pk': 3435, 'rank': 198, 'definition': ' n. 信息, 情报, 新闻, 资料, 询问'}], 'rank': 198},{'content':...},...]
words_data=res_dict['data']#把data的数据提取出来
#print(words_data)#[{'content': 'president', 'pk': 4775, 'definition_choices': [{'pk': 4147, 'rank': 249, 'definition': ' n. 音乐,伴奏,美妙的声音'}, {'pk': 4117, 'rank': 133, 'definition': ' n. 母亲'}, {'pk': 4775, 'rank': 188, 'definition': ' n. (国家)总统,(公司)总裁,总经理,社长(日本)'}, {'pk': 3054, 'rank': 122, 'definition': ' vi. 发生,碰巧,出现,偶然遇到'}], 'rank': 188}, {'content': 'experience', 'pk': 2483, 'definition_choices': [{'pk': 283, 'rank': 362, 'definition': ' n. 基底,(支持、收入、力量等的)基础'}, {'pk': 1836, 'rank': 279, 'definition': ' n. 克制,控制,管制,操作装置'}, {'pk': 2483, 'rank': 269, 'definition': ' n. 经历, 经验'}, {'pk': 1886, 'rank': 380, 'definition': ' n. 封面, 盖子, 表面'}], 'rank': 269}, ...]
datas_know=[]
words_know=[]
words_notknow=[]
num=0
for data in words_data:
num+=1
word=data['content']
word_know=input('{}.认识{}吗?(认识的输入y,不认识请直接按Enter):'.format(num,word))
if word_know=='y':
words_know.append(word)
datas_know.append(data)
else:
words_notknow.append(word)
#print(words_know)#['political', 'teach', 'fill', 'trial', 'crowd', ...]
#print(words_notknow)#['blanket', 'ballot', 'praise', 'inspiration', 'fisherman', ...]
#print(datas_know)#[{'content': 'political', 'pk': 4708, 'definition_choices': [{'pk': 4115, 'rank': 72, 'definition': ' adv.最,最多(大); much的最高级; 非常,很; 几乎'}, {'pk': 4708, 'rank': 169, 'definition': ' adj. 政治的, 政党的, 派系斗争的, 有政治头脑的'}, {'pk': 6057, 'rank': 2, 'definition': ' pron. 那'}, {'pk': 4109, 'rank': 33, 'definition': ' adv.更,更多; 达到或处于更大的范围或程度; 此外,更加'}], 'rank': 169},...]
print('现在检测你是否真的认识这些单词。')
right=[]
wrong=[]
for data in datas_know:
word=data['content']
print('A:'+data['definition_choices'][0]['definition'])
print('B:'+data['definition_choices'][1]['definition'])
print('C:'+data['definition_choices'][2]['definition'])
print('D:'+data['definition_choices'][3]['definition'])
choice=input('{}的意思是:'.format(word))
dict={'A':data['definition_choices'][0]['rank'],'B':data['definition_choices'][1]['rank'],'C':data['definition_choices'][2]['rank'],'D':data['definition_choices'][3]['rank']}
if dict[choice]==data['rank']:
right.append(word)
else:
wrong.append(word)
#print(right)
#print(wrong)
f=open('错题集.txt','a+',encoding='utf-8-sig')
f.write('你记错的单词有:\n')
for word in wrong:
#print(word,end='\t')
f.write(word)
f.write('\t')
f.write('\n你不认识的单词有:\n')
for word in words_notknow:
#print(word,end='\t')
f.write(word)
f.write('\t')
f.close()
len(words_data)
tiku_choice
print('在{}个{}词汇中,你认识{}个,不认识{}个,掌握了{}个,做错了{}个'.format(len(words_data),tiku_choice,len(words_know),len(words_notknow),len(right),len(wrong)))
参考答案:
import requests
link = requests.get('https://www.shanbay.com/api/v1/vocabtest/category/')
#先用requests下载链接。
js_link = link.json()
#解析下载得到的内容。
bianhao = int(input('''请输入你选择的词库编号,按Enter确认
1,GMAT 2,考研 3,高考 4,四级 5,六级
6,英专 7,托福 8,GRE 9,雅思 10,任意
>'''))
#让用户选择自己想测的词库,输入数字编号。int()来转换数据类型
ciku = js_link['data'][bianhao-1][0]
#利用用户输入的数字编号,获取题库的代码。如果以输入“高考”的编号“3”为例,那么ciku的值就是,在字典js_link中查找data的值,data是一个list,查找它的第bianhao-1,也就是第2个元素,得到的依然是一个list,再查找该list的第0个元素。最后得到的就是我们想要的NCEE。
test = requests.get('https://www.shanbay.com/api/v1/vocabtest/vocabularies/?category='+ciku)
#下载用于测试的50个单词。
words = test.json()
#对test进行解析。
danci = []
#新增一个list,用于统计用户认识的单词
words_knows = []
#创建一个空的列表,用于记录用户认识的单词。
not_knows = []
#创建一个空的列表,用于记录用户不认识的单词。
print ('测试现在开始。如果你认识这个单词,请输入Y,否则直接敲Enter:')
n=0
for x in words['data']:
#启动一个循环,循环的次数等于单词的数量。
n=n+1
print ("\n第"+str(n)+'个:'+x['content'])
#加一个\n,用于换行。
answer = input('认识请敲Y,否则敲Enter:')
#让用户输入自己是否认识。
if answer == 'Y':
#如果用户认识:
danci.append(x['content'])
words_knows.append(x)
#就把这个单词,追加进列表words_knows。
else:
#否则
not_knows.append(x)
#就把这个单词,追加进列表not_knows。
print ('\n在上述'+str(len(words['data']))+'个单词当中,有'+str(len(danci))+'个是你觉得自己认识的,它们是:')
print(danci)
print ('现在我们来检测一下,你有没有真正掌握它们:')
wrong_words = []
right_num = 0
for y in words_knows:
print('\n\n'+'A:'+y['definition_choices'][0]['definition'])
#我们改用A、B、C、D,不再用rank值,下同
print('B:'+y['definition_choices'][1]['definition'])
print('C:'+y['definition_choices'][2]['definition'])
print('D:'+y['definition_choices'][3]['definition'])
xuanze = input('请选择单词\"'+y['content']+'\"的正确翻译(填写数字即可):')
dic = {'A':y['definition_choices'][0]['rank'],'B':y['definition_choices'][1]['rank'],'C':y['definition_choices'][2]['rank'],'D':y['definition_choices'][3]['rank']}
#我们创建一个字典,搭建起A、B、C、D和四个rank值的映射关系。
if dic[xuanze] == y['rank']:
#此时dic[xuanze]的内容,其实就是rank值,此时的代码含义已经和之前的版本相同了。
right_num += 1
else:
wrong_words.append(y)
print ('现在,到了公布成绩的时刻:')
print ('在'+str(len(words['data']))+'个'+js_link['data'][bianhao-1][1]+'词汇当中,你认识其中'+str(len(danci))+'个,实际掌握'+str(right_num)+'个,错误'+str(len(wrong_words))+'个。')
#这是句蛮复杂的话,对照前面的代码和json文件你才能理解它。一个运行示例是:在50个高考词汇当中,你认识其中30个,实际掌握25个,错误5个。
save = input ('是否打印并保存你的错词集?填入Y或N: ')
#询问用户,是否要打印并保存错题集。
if save == 'Y':
#如果用户说是:
f = open('错题集.txt', 'a+')
#在当前目录下,创建一个错题集.txt的文档。
print ('你记错的单词有:')
f.write('你记错的单词有:\n')
#写入"你记错的单词有:\n"
m=0
for z in wrong_words:
#启动一个循环,循环的次数等于,用户的错词数:
m=m+1
print (z['content'])
#打印每一个错词。
f.write(str(m+1) +'. '+ z['content']+'\n')
#写入序号,写入错词。
print ('你不认识的单词有:')
f.write('你没记住的单词有:\n')
#写入"你没记住的单词有:\n"
s=0
for x in not_knows:
#启动一个循环,循环的次数等于,用户不认识的单词数。
print (x['content'])
#打印每一个不认识的单词。
f.write(str(s+1) +'. '+ x['content']+'\n')
#写入序号,写入用户不认识的词汇。
print ('错词和没记住的词已保存至当前文件目录下,下次见!')
#告诉用户,文件已经保存好。
#在网页版终端运行时,文件会被写在课程的服务器上,你看不到,但它的确已经存在。
else:
#如果用户不想保存:
print('下次见!')
#输出“下次见!”