• Coding

    Python Learning: Automate Boring Stuff with Python | Chapter 15: Scheduled Web Comic Downloader

    Book The world is indeed comic, but the joke is on mankind. H. P. Lovecraft true.. true.. 6:23:15 PM I made different functions to handle a specific (one) task; Division of labor [sourcecode language=”python”]</p> <p>#usage python download_lefthand_comic.py<br> import logging, os, requests, bs4,time, re</p> <p>logging.basicConfig(level=logging.DEBUG, format="%(asctime)s – %(levelname)s – %(message)s")</p> <p>comic_directory = os.path.join("c:\\", "comicbooks", "lefthandedtoons")<br> comic_directory = os.path.abspath(comic_directory)</p> <p>base_url = "http://www.lefthandedtoons.com"</p> <p>def handle_each_comic(soup: bs4.BeautifulSoup):<br> comic_image = soup.find(class_="comicimage")<br> if comic_image:<br> img_src = comic_image.get("src")<br> comic_name = soup.find(class_="comictitle").find("a")<br> ext_expression = re.compile(r"\.(\w{3})$")<br> comic_name = comic_name.get_text()<br> logging.info("Comic: {} has src {}".format(comic_name, img_src))<br> comic_image_ext = ext_expression.search(img_src)<br> comic_image_ext = (comic_image_ext.groups()[0])</p> <p> #download image<br> if not os.path.exists(comic_directory):<br> os.makedirs(comic_directory)<br> logging.info("Comic Directory Created: " + comic_directory)<br> try:<br> res = requests.get(img_src)<br> res.raise_for_status()<br> #create…

  • Coding

    Python Learning: Automate Boring Stuff with Python | Chapter 14: Excel-to-CSV Converter

    And where I excel is ridiculous, sickening, work ethic. You know, while the other guy’s sleeping? I’m working. Will Smith I like will smith!! 12:50:11 AM   This task was unexpectedly difficult for me, so I am not sure if it is correct [sourcecode language=”python”] #USAGE python excel_2_csv.py import openpyxl, os, sys, logging, csv logging.basicConfig(level=logging.DEBUG, format="%(asctime)s – %(levelname)s – %(message)s") #TODO:READ ALL EXCEL WORKBOOKS IN CURRENT WORKING DIRECTORY for file in os.listdir(): if not file.endswith(".xlsx"): continue if file.startswith("censuspopdata"): continue #TODO CREATE READ WORKBOOK wb = openpyxl.load_workbook(os.path.join(".", file)) ws = wb.sheetnames if not ws or len(ws) <= 0: continue #TOOD:LOOP THROUGH SHEETS for sheetName in ws: #TODO:CREATE NAME OF NEW CSV FILE…

Enjoy this blog? Please spread the word :)

error: Content is protected !!