]> git.armaanb.net Git - phrases.git/blobdiff - extract.py
add argument parsing, fix program logic
[phrases.git] / extract.py
index a76fbfbd991d599b789ef062097df750237bddf8..4e6c1f63544e13e9e434718350714ddd080b9382 100755 (executable)
@@ -2,27 +2,35 @@
 # Extract Latin famous phrases from wikipedia
 # Armaan Bhojwani 2020
 
-from bs4 import BeautifulSoup
-import requests
+import argparse
+import sys
 import csv
+import requests
+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:
+def main(args=sys.argv[1:]):
+    # Argument parsing
+    parser = argparse.ArgumentParser(
+        description="Generate CSV file of Latin famous phrases from Wikipedia.")
+    parser.add_argument("-o", "--output",
+                       default="phrases.csv",
+                       help="set custom output file location")
+    args = parser.parse_args()
+
+    url = ('https://en.wikipedia.org/w/index.php?title=List_of_Latin_phrases_('
+          'full)&oldid=986793908')
+    soup = BeautifulSoup(requests.get(url).content, "html.parser")
+    i = 0 # For the phrase id
+
+    with open(args.output, 'w') as f:
         writer = csv.writer(f, lineterminator="\n")
 
-        i = 0 # For the phrase id
-
         # write header
         headers = ['id', 'Latin', 'English', 'Notes', 'Length']
         writer.writerow(headers)
 
         # iterate through the tables in the page
+        list_table = soup.find_all("table", attrs={"class":"wikitable"})
         for table in list_table:
             for row in table.tbody.find_all("tr", recursive=False):
                 cell = row.find_all("td", recursive=False)