X-Git-Url: https://git.armaanb.net/?p=phrases.git;a=blobdiff_plain;f=extract.py;h=4a0a6b00cb3b44a01e3b02e54506dcd6197379b7;hp=a76fbfbd991d599b789ef062097df750237bddf8;hb=HEAD;hpb=3e4ee2d1a36d23079d68d3dda8b004bbd7cc8106 diff --git a/extract.py b/extract.py index a76fbfb..4a0a6b0 100755 --- a/extract.py +++ b/extract.py @@ -1,45 +1,75 @@ #!/usr/bin/env python3 -# Extract Latin famous phrases from wikipedia -# Armaan Bhojwani 2020 +# Extract Latin famous phrases from Wikipedia +# Armaan Bhojwani 2021 -from bs4 import BeautifulSoup +import argparse +import sqlite3 import requests -import csv +from bs4 import BeautifulSoup -def main(): - url = 'https://en.wikipedia.org/wiki/List_of_Latin_phrases_(full)' - response = requests.get(url) - html = response.content - soup = BeautifulSoup(html, "html.parser") - list_table = soup.find_all("table", attrs={"class":"wikitable"}) - with open('phrases.csv', 'w') as f: - writer = csv.writer(f, lineterminator="\n") +def parse_args(): + parser = argparse.ArgumentParser( + description="Generate database of Latin famous phrases from Wikipedia") + parser.add_argument("-o", "--output", + default="phrases.db", + help="set custom output file location") + parser.add_argument("-v", "--version", + action="version", + version="phrases-extract 1.0.3") + return parser.parse_args() + + +def get_html(url): + print("downloading webpage") + return BeautifulSoup(requests.get(url).content, "html.parser") + - i = 0 # For the phrase id +def prep_database(): + print("prepping database") + c.execute("DROP TABLE IF EXISTS phrases") + c.execute("""CREATE TABLE phrases( + id INTEGER, + latin TEXT, + english TEXT, + notes TEXT, + length INTEGER)""") - # write header - headers = ['id', 'Latin', 'English', 'Notes', 'Length'] - writer.writerow(headers) - # iterate through the tables in the page - for table in list_table: - for row in table.tbody.find_all("tr", recursive=False): - cell = row.find_all("td", recursive=False) - rowc = [] +def fill_database(list_table): + i = 0 # phrase id + print("iterating through tables") + for table in list_table: + for row in table.tbody.find_all("tr", recursive=False): + cell = row.find_all("td", recursive=False) + if len(cell) > 2: + print(i, end="\r") - rowc.append(i) # append phrase id + latin = (cell[0].get_text(" ", strip=True)).rstrip() + english = (cell[1].get_text(" ", strip=True)).rstrip() + notes = (cell[2].get_text(" ", strip=True)).rstrip() - # add cell content - for content in cell: - text = (content.get_text(" ", strip=True)).rstrip() - rowc.append(text) + c.execute("""INSERT INTO phrases + (id, latin, english, notes, length) + VALUES(?, ?, ?, ?, ?)""", + (i, latin, english, notes, len(latin))) + conn.commit() + i = i + 1 + + +def get_tables(): + url = ("""https://en.wikipedia.org/w/index.php?title=List_of_Latin_phrases_( + full)&oldid=986793908""") + return get_html(url).find_all("table", attrs={"class": "wikitable"}) + + +def main(): + prep_database() + fill_database(get_tables()) - if len(rowc) > 1: - rowc.append(len(rowc[1])) - writer.writerow(rowc) - i = i + 1 - f.close() if __name__ == "__main__": + args = parse_args() + conn = sqlite3.connect(args.output) + c = conn.cursor() main()