要求
實現功能:利用扇貝網: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('下次見!')
#輸出“下次見!”
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
