Skip to content

Instantly share code, notes, and snippets.

Created June 11, 2021 00:40
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
import wikipedia
from bs4 import BeautifulSoup, Tag
import datetime
import urllib.parse
from ankisync2.apkg import Apkg
from ankisync2.anki20 import db
if __name__ == "__main__":
tdelta = datetime.timedelta(milliseconds=500)
apkg = Apkg("casp_plus_003_terms.apkg")
soup = BeautifulSoup()
anchor_tag = soup.new_tag("a")
front_of_card = ""
back_of_card = ""
wiki_search = ""
wiki_page = None
summary_of_card = ""
print("# CASP+ 003 Terms")
for note in db.Notes.filter():
for k, v in
if v == "Front":
front_of_card = k
if v == "Back":
back_of_card = k
if bool(BeautifulSoup(back_of_card, "html.parser").find()):
wiki_search = BeautifulSoup(back_of_card, "html.parser").contents[0]
wiki_search = back_of_card.replace('/', '')
wikipedia.set_rate_limiting(True, min_wait=tdelta)
answer_wiki =, results=1)
wiki_page =, auto_suggest=False, redirect=True)
anchor_tag.string = back_of_card
anchor_tag["href"] = wiki_page.url
summary_of_card = wiki_page.summary
except wikipedia.exceptions.DisambiguationError as e:
anchor_tag.string = back_of_card
google_search_string = urllib.parse.quote_plus(back_of_card)
anchor_tag["href"] = f"{google_search_string}"
except wikipedia.exceptions.PageError as e: None
print(f"## {front_of_card}")
ankisync2 == 0.3.2
wikipedia == 1.4.0
beautifulsoup4 == 4.9.3
Copy link

This is helpful for pulling Wikipedia page links and summaries by searching the contents from the "back" of a given card. Hope this helps someone else too.

Copy link

rogeruiz commented Jun 11, 2021

Run this with:

# Create the directories we'll need for processing from MD to APKG.
mkdir -p input output

# Create the input/ file.
pip install -r requirements.txt
python ./ 2>/dev/null > input/

# Edit the results from the script.
$EDITOR input/

# Install tooling for Markdown to Anki Decks processing.
pip install markdown-anki-decks

# Create the new .apkg file.
mdankideck input output

Copy link

This is the main repository that leverages this script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment