Anki 教學 - 批量匯入卡片
在製作 Anki 牌組過程中,批量製作卡片算是一個關鍵的技術。
各位可以稍稍參考以下影片,本人也很久沒碰 Anki 了,不過這樣的生疏,錄影出來也是讓大家更能體會大量製作牌組時可能會遇到的一些問題。
影片中並沒有詳細解釋如何從網路上下載與單字相關的圖片、聲音檔。
以下是我當時寫的兩個小程式,給有興趣大家的參考(大家還是要依照情況修改)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8 | |
import urllib, re, shutil, urllib2, os | |
#設定網站,網頁是XXX.html | |
website = 'http://all-free-download.com/free-photos/' | |
#在網站中尋找圖片 | |
prog = re.compile(' width="200" \s+class="pixazza_0" .*? src="(.*?)">',re.I|re.M|re.S) | |
#下載圖片的測試用的資料夾 | |
#folderpath = r'D:\test\py\copytree' | |
folderpath = r'D:\test\py\copytree' | |
#分出缺少的圖片 | |
srcfile = r'D:\test\py\CannotFind2.txt' | |
with open(srcfile,'r') as k: | |
docum = k.read().split('\n') | |
#假裝不是機器人 | |
opener = urllib2.build_opener() | |
opener.addheaders = [('User-agent', 'Mozilla/5.0')] | |
opener.addheaders = [('Referer', 'http://all-free-download.com/free-photos')] | |
#一一尋找圖片 | |
for fs in docum: | |
if fs.endswith('.jpg'): | |
query = fs[:-4]+'.html' | |
content = opener.open( website + query ).read() | |
# print content #看有否抓到內容 | |
url = prog.search(content).group(1) | |
print url #看有否抓到圖片 | |
newloc = os.path.join(folderpath,fs) | |
#下載圖片 | |
data = urllib.urlretrieve(url,newloc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8 | |
import urllib, re, urllib2, os | |
#下載http://dictionary.reference.com/browse/ 上面的發音 | |
#設定網站,網頁是XXX.html | |
website = 'http://dictionary.reference.com/browse/' | |
#在網站中尋找聲音 | |
prog = re.compile('audio="(http://.*?\.mp3)"',re.I|re.M|re.S) | |
#下載聲音的測試用的資料夾 | |
#folderpath = r'D:\test\py\copytree\test' | |
folderpath = r'D:\test\py\copytree\test' | |
#分出缺少的聲音 | |
srcfile = r'D:\test\py\CannotFind2.txt' | |
with open(srcfile,'r') as k: | |
docum = k.read().split('\n') | |
#假裝不是機器人 | |
opener = urllib2.build_opener() | |
opener.addheaders = [('User-agent', 'Mozilla/5.0')] | |
opener.addheaders = [('Referer', 'http://dictionary.reference.com')] | |
#一一尋找聲音 | |
for fs in docum: | |
if fs.endswith('.mp3'): | |
query = fs[:-4] | |
# print query | |
content = opener.open( website + query ).read() | |
# print content #看有否抓到內容 | |
# 用 try 可以防止錯誤而停止 | |
try : | |
prog.search(content).group(1) | |
#print url #看有否抓到聲音 | |
url = prog.search(content).group(1) | |
# print url | |
newloc = os.path.join(folderpath,fs) | |
#下載圖片 | |
data = urllib.urlretrieve(url,newloc) | |
except: | |
pass |
留言
張貼留言